Skip to content

Commit cd864f8

Browse files
committed
Create LCP08.剧情触发时间.js
1 parent a4efba7 commit cd864f8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

LCP08.剧情触发时间.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number[][]} increase
3+
* @param {number[][]} requirements
4+
* @return {number[]}
5+
*/
6+
var getTriggerTime = function(increase, requirements) {
7+
const a = [0];
8+
const b = [0];
9+
const c = [0];
10+
for (const [x, y, z] of increase) {
11+
a.push(a[a.length - 1] + x);
12+
b.push(b[b.length - 1] + y);
13+
c.push(c[c.length - 1] + z);
14+
}
15+
16+
function binarySearch(arr, n) {
17+
let left = 0;
18+
let right = arr.length - 1;
19+
while (left <= right) {
20+
const mid = (left + right) >> 1;
21+
if (arr[mid] >= n && (mid === 0 || arr[mid - 1] < n)) {
22+
return mid;
23+
}
24+
if (arr[mid] >= n) {
25+
right = mid - 1;
26+
} else {
27+
left = mid + 1;
28+
}
29+
}
30+
return -1;
31+
}
32+
33+
return requirements.map(([x, y, z]) => {
34+
const results = [
35+
binarySearch(a, x),
36+
binarySearch(b, y),
37+
binarySearch(c, z),
38+
]
39+
return results.includes(-1) ? -1 : Math.max(...results);
40+
})
41+
};

0 commit comments

Comments
 (0)