Skip to content

Commit 5b4cfd5

Browse files
authored
Create 2280-minimum-lines-to-represent-a-line-chart.js
1 parent d340025 commit 5b4cfd5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[][]} stockPrices
3+
* @return {number}
4+
*/
5+
var minimumLines = function(stockPrices) {
6+
let res = 1
7+
const ma = stockPrices
8+
const n = ma.length
9+
if(n === 0 || n === 1) return 0
10+
ma.sort((a, b) => a[0] - b[0])
11+
const eps = 1e-30
12+
let dx = ma[1][0] - ma[0][0], dy = ma[1][1] - ma[0][1]
13+
for(let i = 2; i < n; i++) {
14+
const cur = ma[i], pre = ma[i - 1]
15+
const dxx = cur[0] - pre[0], dyy = cur[1] - pre[1]
16+
if(BigInt(dxx) * BigInt(dy) !== BigInt(dx) * BigInt(dyy)) res++
17+
dx = dxx
18+
dy = dyy
19+
}
20+
21+
return res
22+
};
23+
24+
function product(p1, p2, p3) {
25+
// 首先根据坐标计算p1p2和p1p3的向量,然后再计算叉乘
26+
// p1p2 向量表示为 (p2.x-p1.x,p2.y-p1.y)
27+
// p1p3 向量表示为 (p3.x-p1.x,p3.y-p1.y)
28+
return (p2.x-p1.x)*(p3.y-p1.y) - (p2.y-p1.y)*(p3.x-p1.x);
29+
}

0 commit comments

Comments
 (0)