|
8 | 8 | If we were to start from the left to the right, we would have seen the longest subsequence on the left side as we are going to the right.
|
9 | 9 | Inorder for us to know what those longest subsequences were, we will need a way to store that, hello dynamic programming.
|
10 | 10 |
|
11 |
| -For each element, we would need look at the numbers less than the current element on the left side. |
| 11 | +For each element, we would need look at the numbers on the left that are less than the current element. |
12 | 12 | Now that we know which numbers those are, we can then look at their corresponding longest subsequence in the dynamic programming array.
|
13 | 13 | From this list, get the longest subsequence.
|
14 |
| -This will tell us what to set as our longest subsequence for this current element + 1. |
15 | 14 |
|
16 | 15 | We are basically building the longest increasing subsequence from the bottom up.
|
17 | 16 |
|
18 | 17 | **Example:**
|
19 |
| - |
20 |
| -I(Input): [10,9,2,5,3,7,101,18] |
21 |
| - |
22 |
| -DP: [1,1,1,1,1,1,1,1] |
23 |
| - |
24 |
| -1. DP[1] = 1 + max([]) **(I[1] not > I[0])** |
25 |
| -2. DP[2] = 1 + max([]) **(I[2] not > I[0],I[1])** |
26 |
| -3. DP[3] = 1 + max(DP[2]) **(I[3] > I[2] and I[3] not > I[0],I[1])** |
27 |
| -4. DP[4] = 1 + max(DP[2]) **(I[4] > I[2] and I[4] not > I[0],I[1],I[3])** |
28 |
| -5. DP[5] = 1 + max(DP[1], DP[2], DP[3]) **(I[5] > I[1],I[2],I[3] and I[5] not > I[0])** |
29 |
| -6. DP[6] = 1 + max(DP[0], DP[1], DP[2], DP[3], DP[4], DP[5]) **(I[6] > all prev numbers)** |
30 |
| -7. DP[7] = 1 + max(DP[0], DP[1], DP[2], DP[3], DP[4], DP[5]) **(I[7] > all prev numbers except I[6])** |
| 18 | +``` |
| 19 | +Input(I): [10,9,2,5,3,7,101,18] |
| 20 | +
|
| 21 | +DP: |
| 22 | +10 [1, 1, 1, 1, 1, 1, 1, 1] |
| 23 | +9 [1, 1, 1, 1, 1, 1, 1, 1] -> DP[1] = 1 + max([]) (I[1] not > I[0]) |
| 24 | +2 [1, 1, 1, 1, 1, 1, 1, 1] -> DP[2] = 1 + max([]) (I[2] not > I[0],I[1]) |
| 25 | +5 [1, 1, 1, 2, 1, 1, 1, 1] -> DP[3] = 1 + max(DP[2]) (I[3] > I[2] and I[3] not > I[0],I[1]) |
| 26 | +3 [1, 1, 1, 2, 2, 1, 1, 1] -> DP[4] = 1 + max(DP[2]) (I[4] > I[2] and I[4] not > I[0],I[1],I[3]) |
| 27 | +7 [1, 1, 1, 2, 2, 3, 1, 1] -> DP[5] = 1 + max(DP[1], DP[2], DP[3]) (I[5] > I[1],I[2],I[3] and I[5] not > I[0]) |
| 28 | +101 [1, 1, 1, 2, 2, 3, 4, 1] -> DP[6] = 1 + max(DP[0], DP[1], DP[2], DP[3], DP[4], DP[5]) (I[6] > all prev numbers) |
| 29 | +18 [1, 1, 1, 2, 2, 3, 4, 4] -> DP[7] = 1 + max(DP[0], DP[1], DP[2], DP[3], DP[4], DP[5]) (I[7] > all prev numbers except I[6]) |
| 30 | +``` |
31 | 31 |
|
32 | 32 | ```
|
33 | 33 | class Solution:
|
|
0 commit comments