Minimum Lines to Represent a Line Chart LeetCode solution in Java

1
class Solution {
2
public int minimumLines(int[][] stockPrices) {
3
if (stockPrices.length == 1) return 0;
4
int count = 1;
5
Arrays.sort(stockPrices, (o1, o2) -> o1[0] - o2[0]);
6
for (int i = 1; i < stockPrices.length - 1; i++) {
7
int x1 = stockPrices[i - 1][0];
8
int y1 = stockPrices[i - 1][1];
9
int x2 = stockPrices[i][0];
10
int y2 = stockPrices[i][1];
11
int x3 = stockPrices[i + 1][0];
12
int y3 = stockPrices[i + 1][1];
13
if (((x3 - x2) * (y2 - y1)) == ((y3 - y2) * (x2 - x1))) continue;
14

15
count++;
16
}
17
return count;
18
}
19
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0