forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0300-longest-increasing-subsequence.cpp
40 lines (32 loc) · 1.19 KB
/
0300-longest-increasing-subsequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
Given int array, return length of longest increasing subsequence
Ex. nums = [10,9,2,5,3,7,101,18] -> 4, [2,3,7,101]
Why DP? 1) Max/min of smth, 2) make decisions based on prev decisions
"Decision": is it worth it to consider this number?
If use may contribute to better LIS, but may also eliminate an even better LIS
Framework to solve DP:
1) Need some function or array that represents ans to the problem (dp array)
2) Way to transition b/w states (recurrence relation), depends on question
3) Need a base case (initial solution for every subproblem)
Recurrence relation: dp[i] = max(dp[j] + 1)
Base case: dp[i] = 1, since every element on its own has an LIS of 1
Time: O(n^2)
Space: O(n)
*/
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, 1);
int result = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
result = max(result, dp[i]);
}
return result;
}
};