Skip to content

Commit 4db9500

Browse files
authored
Update 2280-minimum-lines-to-represent-a-line-chart.js
1 parent faae973 commit 4db9500

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

2280-minimum-lines-to-represent-a-line-chart.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
/**
2+
* @param {number[][]} stockPrices
3+
* @return {number}
4+
*/
5+
const minimumLines = function(stockPrices) {
6+
let res = 1
7+
const n = stockPrices.length
8+
if(n === 1) return 0
9+
stockPrices.sort((a, b) => a[0] - b[0])
10+
for(let i = 2; i < n; i++) {
11+
const cur = stockPrices[i], p = stockPrices[i - 1], pp = stockPrices[i - 2]
12+
if(chk(pp, p, cur)) continue
13+
else res++
14+
}
15+
16+
17+
return res
18+
};
19+
20+
function chk(p1, p2, p3) {
21+
const bi = BigInt
22+
// (y3 - y1) / (x3 - x1) == (y2 - y1) / (x2 - x1)
23+
const [x1, y1] = p1, [x2, y2] = p2, [x3, y3] = p3
24+
return (bi(y3) - bi(y1)) * (bi(x2) - bi(x1)) === (bi(y2) - bi(y1)) * (bi(x3) - bi(x1))
25+
}
26+
27+
28+
29+
// another
30+
131
/**
232
* @param {number[][]} stockPrices
333
* @return {number}

0 commit comments

Comments
 (0)