Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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
dayi
are distinct.class Solution {
public:
int minimumLines(vector<vector<int>>& stk) {
sort(stk.begin(),stk.end());
int n = stk.size();
if(n==1)
return 0;
int cnt = 1;
for(int i=2;i<n;i++){
long x1 = stk[i][0],x2 = stk[i-1][0],x3 = stk[i-2][0];
long y1 = stk[i][1],y2 = stk[i-1][1],y3 = stk[i-2][1];
long diff1 = (y3-y2) * (x2-x1);
long diff2 = (y2-y1) * (x3-x2);
if(diff1 != diff2)
cnt++;
}
return cnt;
}
};
public int minimumLines(int[][] A) {
int n = A.length, res = n - 1;
Arrays.sort(A, (a, b) -> Integer.compare(a[0], b[0]));
for (int i = 1; i < n - 1; ++i)
if (1L * (A[i][0] - A[i - 1][0]) * (A[i + 1][1] - A[i][1]) == 1L * (A[i + 1][0] - A[i][0]) * (A[i][1] - A[i - 1][1]))
res--;
return res;
}
def minimumLines(self, A: List[List[int]]) -> int:
n = len(A)
res = n - 1
A.sort()
for i in range(1, n - 1):
a, b, c = A[i-1], A[i], A[i+1]
if (b[0] - a[0]) * (c[1] - b[1]) == (c[0] - b[0]) * (b[1] - a[1]):
res -= 1
return res
In our experience, we suggest you solve this Minimum Lines to Represent a Line Chart LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimum Lines to Represent a Line Chart LeetCode Solution
I hope this Minimum Lines to Represent a Line Chart LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>