Skip to content

Commit a42832b

Browse files
authored
Update lengthOfLIS.cpp
1 parent 2932a20 commit a42832b

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

Dynamic-Programming/lengthOfLIS.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,21 @@ class Solution {
1616
public:
1717
int lengthOfLIS(vector<int>& nums) {
1818
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;
19+
if(n == 1) return 1;
20+
vector<int> dp(n+1, 1);
21+
int maxLength = 1;
22+
int i, j;
23+
for(i = 1; i < n;++i) {
24+
j = i-1;
25+
while(j >= 0) {
26+
if(nums[j] < nums[i]) dp[i] = max(dp[i], 1 + dp[j]);
27+
--j;
3128
}
32-
dp[i]=1 + maxLIS;
33-
res = max(res, dp[i]);
29+
30+
maxLength = max(maxLength, dp[i]);
3431
}
35-
return res;
32+
return maxLength;
33+
3634
}
3735
};
36+

0 commit comments

Comments
 (0)