Skip to content

Commit 961713f

Browse files
authored
Create 1964-find-the-longest-valid-obstacle-course-at-each-position.kt
1 parent f0e2f0e commit 961713f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
fun longestObstacleCourseAtEachPosition(obstacles: IntArray): IntArray {
3+
val dp = IntArray(obstacles.size) { Integer.MAX_VALUE }
4+
val res = IntArray(obstacles.size)
5+
6+
fun binarySearch(n: Int): Int {
7+
var l = 0
8+
var r = dp.lastIndex
9+
10+
while (l < r) {
11+
val m = l + (r - l) / 2
12+
if (n >= dp[m]) l = m + 1
13+
else r = m
14+
}
15+
16+
return l
17+
}
18+
19+
for ((i, n) in obstacles.withIndex()) {
20+
val insert = binarySearch(n)
21+
res[i] = insert + 1
22+
dp[insert] = n
23+
}
24+
25+
return res
26+
}
27+
}

0 commit comments

Comments
 (0)