Skip to content

Commit e76ec4d

Browse files
authored
Create 1964-find-the-longest-valid-obstacle-course-at-each-position.js
1 parent a9fc36b commit e76ec4d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[]} obstacles
3+
* @return {number[]}
4+
*/
5+
const longestObstacleCourseAtEachPosition = function(obstacles) {
6+
const n = obstacles.length
7+
const stack = [], res = Array(n).fill(0)
8+
let m = 0
9+
let j = 0;
10+
for (let x of obstacles) {
11+
let i = chk(x);
12+
if (i == m) {
13+
++m;
14+
stack.push(x);
15+
} else {
16+
stack[i] = x;
17+
}
18+
res[j++] = i + 1;
19+
}
20+
return res;
21+
function chk(x) {
22+
if (m && stack[m - 1] <= x) return m;
23+
let l = 0, r = m - 1;
24+
while (l < r) {
25+
let m = (l + r) >> 1;
26+
if (stack[m] > x) {
27+
r = m;
28+
} else {
29+
l = m + 1;
30+
}
31+
}
32+
return l;
33+
}
34+
};
35+

0 commit comments

Comments
 (0)