Skip to content

Commit 638415b

Browse files
committed
-Add: Longest Increasing Subsequence binary search
1 parent 54f3bdc commit 638415b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
fun lengthOfLIS(nums: IntArray): Int {
3+
val sub = mutableListOf<Int>()
4+
nums.forEach { num ->
5+
if (sub.isEmpty() || sub.last() < num) {
6+
sub.add(num)
7+
} else {
8+
val bin = sub.binarySearch(num)
9+
val i = if (bin >= 0) bin else -bin - 1
10+
sub[i] = num
11+
}
12+
}
13+
return sub.size
14+
} //TC: O(nlogn) SC: O(n)
15+
}

0 commit comments

Comments
 (0)