Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 935d8e9

Browse files
Updated better solution for question 300.
1 parent 57817d1 commit 935d8e9

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

‎leetcode/medium/300_longest_increasing_subsequence.md‎

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,60 +36,57 @@ class Solution:
3636
return max(dp, default=0)
3737
```
3838

39-
## Dynamic Programming with Binary Search Solution
39+
## Binary Search Solution
4040
- Run-time: O(Nlog(N))
4141
- Space: O(N)
4242
- N = Number of elements in array
4343

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.
4748

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].
5352

5453
```
5554
Input: [1,3,6,7,9,4,10,5,6]
5655
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]
6766
```
6867

6968
```
7069
class Solution(object):
7170
def lengthOfLIS(self, nums):
7271
7372
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
7574
insert_idx = -1
7675
while left <= right:
7776
mid_idx = left + ((right - left) // 2)
78-
if memo[mid_idx] >= n:
77+
if sorted_subseq[mid_idx] >= n:
7978
insert_idx = mid_idx
8079
right = mid_idx - 1 # go left
8180
else: # go right
8281
left = mid_idx + 1
8382
return insert_idx
8483
85-
memo = [0] * len(nums)
86-
end_idx = -1
84+
sorted_subseq = list()
8785
for n in nums:
8886
idx = binary_search_insertion_idx(n)
8987
if idx == -1: # no number found greater than n
90-
end_idx += 1
91-
memo[end_idx] = n
88+
sorted_subseq.append(n)
9289
else: # found a number that is greater than n
93-
memo[idx] = n
94-
return end_idx + 1
90+
sorted_subseq[idx] = n
91+
return len(sorted_subseq)
9592
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /