Skip to content

Commit 3f01dfe

Browse files
authored
Update 1964-find-the-longest-valid-obstacle-course-at-each-position.js
1 parent 626e396 commit 3f01dfe

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

1964-find-the-longest-valid-obstacle-course-at-each-position.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/**
2+
* @param {number[]} obstacles
3+
* @return {number[]}
4+
*/
5+
const longestObstacleCourseAtEachPosition = function(obstacles) {
6+
const n = obstacles.length, res = [], stk = []
7+
for (let i = 0; i < n; i++) {
8+
const cur = obstacles[i]
9+
let idx = chk(cur)
10+
if (idx === stk.length) {
11+
stk.push(cur)
12+
} else {
13+
stk[idx] = cur
14+
}
15+
res.push(++idx)
16+
}
17+
18+
return res
19+
20+
function chk(val) {
21+
let l = 0, r = stk.length
22+
while(l < r) {
23+
const mid = ~~((l + r) / 2)
24+
if(stk[mid] <= val) l = mid + 1
25+
else r = mid
26+
}
27+
return l
28+
}
29+
};
30+
31+
// another
32+
133
/**
234
* @param {number[]} obstacles
335
* @return {number[]}

0 commit comments

Comments
 (0)