You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: leetcode/medium/300_longest_increasing_subsequence.md
+24-27
Original file line number
Diff line number
Diff line change
@@ -36,60 +36,57 @@ class Solution:
36
36
return max(dp, default=0)
37
37
```
38
38
39
-
## Dynamic Programming with Binary Search Solution
39
+
## Binary Search Solution
40
40
- Run-time: O(Nlog(N))
41
41
- Space: O(N)
42
42
- N = Number of elements in array
43
43
44
-
Slightly modifying the previous approach.
45
-
We can use a dynamic programming 1d array of 0s but instead store the subsequence themselves instead of what the previous longest subsequence were.
46
-
The 1d array can be thought of as keeping a sorted array within itself, there will be a subset of indexes that would represent the longest subsequence.
44
+
If we instead used a sorted array to keep a subsequence, we can decide whether or not we can create a larger subsequence with binary search.
45
+
For each n, we can binary search the sorted array for the left-most number that is larger or equal to n.
46
+
If that number is found, we can replace that number with n.
47
+
If no number is found, we can append n to the end of the array, this basically means we can create a larger subsequence.
47
48
48
-
With this sorted array, we can then binary search it to find where a given n should be placed.
49
-
We want to find a number that is greater than or equal to n, then replace that number in the array.
50
-
If a number isn't found, we can increase the range of indexes in the sorted array by 1 and add the new n at the end.
51
-
By increasing the sorted array by 1, it shows that there is a longer subsequence.
52
-
This won't exactly tell us the 'actual' subsequence because of the replacement and ordering but works since the question only cares about what is the longest subsequence.
49
+
Since we replace numbers in the sorted array, the sorted array does not represent the 'actual' longest subsequence.
50
+
We need to replace numbers in the array because we can potentially create a larger subsequence.
51
+
For example, given an input of [1,2,3,99,4,5,6], when we reach number 5, if we didn't replace 99 in the sorted array, we can never increase the subsequence by 1 when the sorted array was [1,2,3,99] instead of [1,2,3,4].
53
52
54
53
```
55
54
Input: [1,3,6,7,9,4,10,5,6]
56
55
57
-
[0, 0, 0, 0, 0, 0, 0, 0, 0]
58
-
[1, 0, 0, 0, 0, 0, 0, 0, 0]
59
-
[1, 3, 0, 0, 0, 0, 0, 0, 0]
60
-
[1, 3, 6, 0, 0, 0, 0, 0, 0]
61
-
[1, 3, 6, 7, 0, 0, 0, 0, 0]
62
-
[1, 3, 6, 7, 9, 0, 0, 0, 0]
63
-
[1, 3, 4, 7, 9, 0, 0, 0, 0]
64
-
[1, 3, 4, 7, 9, 10, 0, 0, 0]
65
-
[1, 3, 4, 5, 9, 10, 0, 0, 0]
66
-
[1, 3, 4, 5, 6, 10, 0, 0, 0]
56
+
[]
57
+
[1]
58
+
[1, 3]
59
+
[1, 3, 6]
60
+
[1, 3, 6, 7]
61
+
[1, 3, 6, 7, 9]
62
+
[1, 3, 4, 7, 9]
63
+
[1, 3, 4, 7, 9, 10]
64
+
[1, 3, 4, 5, 9, 10]
65
+
[1, 3, 4, 5, 6, 10]
67
66
```
68
67
69
68
```
70
69
class Solution(object):
71
70
def lengthOfLIS(self, nums):
72
71
73
72
def binary_search_insertion_idx(n):
74
-
left, right = 0, end_idx
73
+
left, right = 0, len(sorted_subseq) - 1 if len(sorted_subseq) != 0 else -1
0 commit comments