|
| 1 | +/** |
| 2 | + * [2280] Minimum Lines to Represent a Line Chart |
| 3 | + * |
| 4 | + * You are given a 2D integer array stockPrices where stockPrices[i] = [dayi, pricei] indicates the price of the stock on day dayi is pricei. A line chart is created from the array by plotting the points on an XY plane with the X-axis representing the day and the Y-axis representing the price and connecting adjacent points. One such example is shown below: |
| 5 | + * <img alt="" src="https://assets.leetcode.com/uploads/2022/03/30/1920px-pushkin_population_historysvg.png" style="width: 500px; height: 313px;" /> |
| 6 | + * Return the minimum number of lines needed to represent the line chart. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * <img alt="" src="https://assets.leetcode.com/uploads/2022/03/30/ex0.png" style="width: 400px; height: 400px;" /> |
| 10 | + * Input: stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]] |
| 11 | + * Output: 3 |
| 12 | + * Explanation: |
| 13 | + * The diagram above represents the input, with the X-axis representing the day and Y-axis representing the price. |
| 14 | + * The following 3 lines can be drawn to represent the line chart: |
| 15 | + * - Line 1 (in red) from (1,7) to (4,4) passing through (1,7), (2,6), (3,5), and (4,4). |
| 16 | + * - Line 2 (in blue) from (4,4) to (5,4). |
| 17 | + * - Line 3 (in green) from (5,4) to (8,1) passing through (5,4), (6,3), (7,2), and (8,1). |
| 18 | + * It can be shown that it is not possible to represent the line chart using less than 3 lines. |
| 19 | + * |
| 20 | + * Example 2: |
| 21 | + * <img alt="" src="https://assets.leetcode.com/uploads/2022/03/30/ex1.png" style="width: 325px; height: 325px;" /> |
| 22 | + * Input: stockPrices = [[3,4],[1,2],[7,8],[2,3]] |
| 23 | + * Output: 1 |
| 24 | + * Explanation: |
| 25 | + * As shown in the diagram above, the line chart can be represented with a single line. |
| 26 | + * |
| 27 | + * |
| 28 | + * Constraints: |
| 29 | + * |
| 30 | + * 1 <= stockPrices.length <= 10^5 |
| 31 | + * stockPrices[i].length == 2 |
| 32 | + * 1 <= dayi, pricei <= 10^9 |
| 33 | + * All dayi are distinct. |
| 34 | + * |
| 35 | + */ |
| 36 | +pub struct Solution {} |
| 37 | + |
| 38 | +// problem: https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/ |
| 39 | +// discuss: https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/?currentPage=1&orderBy=most_votes&query= |
| 40 | + |
| 41 | +// submission codes start here |
| 42 | + |
| 43 | +impl Solution { |
| 44 | + pub fn minimum_lines(stock_prices: Vec<Vec<i32>>) -> i32 { |
| 45 | + 0 |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// submission codes end |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod tests { |
| 53 | + use super::*; |
| 54 | + |
| 55 | + #[test] |
| 56 | + #[ignore] |
| 57 | + fn test_2280_example_1() { |
| 58 | + let stock_prices = vec![ |
| 59 | + vec![1, 7], |
| 60 | + vec![2, 6], |
| 61 | + vec![3, 5], |
| 62 | + vec![4, 4], |
| 63 | + vec![5, 4], |
| 64 | + vec![6, 3], |
| 65 | + vec![7, 2], |
| 66 | + vec![8, 1], |
| 67 | + ]; |
| 68 | + |
| 69 | + let result = 3; |
| 70 | + |
| 71 | + assert_eq!(Solution::minimum_lines(stock_prices), result); |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + #[ignore] |
| 76 | + fn test_2280_example_2() { |
| 77 | + let stock_prices = vec![vec![3, 4], vec![1, 2], vec![7, 8], vec![2, 3]]; |
| 78 | + |
| 79 | + let result = 1; |
| 80 | + |
| 81 | + assert_eq!(Solution::minimum_lines(stock_prices), result); |
| 82 | + } |
| 83 | +} |
0 commit comments