We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2932a20 commit a42832bCopy full SHA for a42832b
Dynamic-Programming/lengthOfLIS.cpp
@@ -16,22 +16,21 @@ class Solution {
16
public:
17
int lengthOfLIS(vector<int>& nums) {
18
int n = nums.size();
19
- vector<int>dp(n, 1);
20
- int j;
21
- dp[n-1] =1;
22
- int res = dp[n-1];
23
- for(int i = n-2; i >=0; --i) {
24
- j = i+1;
25
- int maxLIS = 0;
26
- while(j < n) {
27
- if(nums[i] < nums[j]) {
28
- maxLIS = max(maxLIS, dp[j]);
29
- }
30
- ++j;
+ if(n == 1) return 1;
+ vector<int> dp(n+1, 1);
+ int maxLength = 1;
+ int i, j;
+ for(i = 1; i < n;++i) {
+ j = i-1;
+ while(j >= 0) {
+ if(nums[j] < nums[i]) dp[i] = max(dp[i], 1 + dp[j]);
+ --j;
31
}
32
- dp[i]=1 + maxLIS;
33
- res = max(res, dp[i]);
+
+ maxLength = max(maxLength, dp[i]);
34
35
- return res;
+ return maxLength;
36
37
};
0 commit comments