1. 程式人生 > 其它 >[LeetCode] 2280. Minimum Lines to Represent a Line Chart

[LeetCode] 2280. Minimum Lines to Represent a Line Chart

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:

Return the minimum number of lines needed to represent the line chart.

Example 1:

Input: stockPrices = [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]
Output: 3
Explanation:
The diagram above represents the input, with the X-axis representing the day and Y-axis representing the price.
The following 3 lines can be drawn to represent the line chart:
- Line 1 (in red) from (1,7) to (4,4) passing through (1,7), (2,6), (3,5), and (4,4).
- Line 2 (in blue) from (4,4) to (5,4).
- Line 3 (in green) from (5,4) to (8,1) passing through (5,4), (6,3), (7,2), and (8,1).
It can be shown that it is not possible to represent the line chart using less than 3 lines.

Example 2:

Input: stockPrices = [[3,4],[1,2],[7,8],[2,3]]
Output: 1
Explanation:
As shown in the diagram above, the line chart can be represented with a single line.

Constraints:

  • 1 <= stockPrices.length <= 105
  • stockPrices[i].length == 2
  • 1 <= dayi, pricei <= 109
  • All dayi are distinct.

表示一個折線圖的最少線段數。

給你一個二維整數陣列 stockPrices ,其中 stockPrices[i] = [dayi, pricei] 表示股票在 dayi 的價格為 pricei 。折線圖 是一個二維平面上的若干個點組成的圖,橫座標表示日期,縱座標表示價格,折線圖由相鄰的點連線而成。請你返回要表示一個折線圖所需要的 最少線段數 。

來源:力扣(LeetCode)
連結:https://leetcode.cn/problems/minimum-lines-to-represent-a-line-chart
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

這是一道數學題,跟斜率 slope 相關。題目給的是股票的價值,用一些點表示,這些點形成一個折線圖,題目問折線圖至少需要幾段。

我的思路是,從第一個點開始,判斷每兩個相鄰的點組成的線段的斜率,比如看第一天和第二天的這段折線的斜率是否跟第二天和第三天的這段折線的斜率一樣。如果一樣,說明這兩條折線其實是一條,如果不一樣,就需要多畫一條折線了。注意這道題的 input 陣列是沒有排序的,所以我們需要先排序再做比較。比較的方式還是注意斜率分母不能為 0 的問題。

時間O(nlogn)

空間O(n)

Java實現

 1 class Solution {
 2     public int minimumLines(int[][] stockPrices) {
 3         int len = stockPrices.length;
 4         // corner case
 5         if (len == 1) {
 6             return 0;
 7         }
 8         
 9         // normal case
10         Arrays.sort(stockPrices, (a, b) -> (a[0] - b[0]));
11         // for (int i = 0; i < len; i++) {
12         //     System.out.println(stockPrices[i][0] + ", " + stockPrices[i][1]);
13         // }
14 
15         int y1 = stockPrices[1][1] - stockPrices[0][1];
16         int x1 = stockPrices[1][0] - stockPrices[0][0];
17         int res = 1;
18         for (int i = 2; i < len; i++) {
19             int y2 = stockPrices[i][1] - stockPrices[i - 1][1];
20             int x2 = stockPrices[i][0] - stockPrices[i - 1][0];
21             if (x1 * y2 != x2 * y1) {
22                 res++;
23                 y1 = y2;
24                 x1 = x2;
25             }
26         }
27         return res;
28     }
29 }

相關題目

1232. Check If It Is a Straight Line

2280. Minimum Lines to Represent a Line Chart

LeetCode 題目總結