Skip to content

Commit 19a16e8

Browse files
authored
Create 2280.Minimum-Lines-to-Represent-a-Line-Chart.cpp
1 parent 6b77c0c commit 19a16e8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int minimumLines(vector<vector<int>>& stockPrices)
4+
{
5+
if (stockPrices.size()==1) return 0;
6+
sort(stockPrices.begin(), stockPrices.end());
7+
8+
int ret = 1;
9+
int n = stockPrices.size();
10+
for (int i=2; i<n; i++)
11+
{
12+
if (!checkAlign(stockPrices, i))
13+
ret++;
14+
}
15+
return ret;
16+
}
17+
18+
bool checkAlign(vector<vector<int>>& stockPrices, int t)
19+
{
20+
int x0 = stockPrices[t-2][0], y0 = stockPrices[t-2][1];
21+
int x1 = stockPrices[t-1][0], y1 = stockPrices[t-1][1];
22+
int x2 = stockPrices[t-0][0], y2 = stockPrices[t-0][1];
23+
24+
return (long long)(y2-y1)*(x1-x0)==(long long)(y1-y0)*(x2-x1);
25+
}
26+
};

0 commit comments

Comments
 (0)