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
dp[index] = 1 + max([dp[j] for j in range(index) if nums[index] > nums[j]], default=0)
38
36
return max(dp, default=0)
39
37
```
38
+
39
+
## Dynamic Programming with Binary Search Solution
40
+
- Run-time: O(Nlog(N))
41
+
- Space: O(N)
42
+
- N = Number of elements in array
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.
47
+
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.
0 commit comments