Skip to content

Commit

Permalink
Update longest_increasing_subsequence.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
caba5 authored Dec 9, 2022
1 parent 456f323 commit efda0eb
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Time: O(n^2)
Space: O(n)
*/

/*
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
Expand All @@ -38,3 +38,21 @@ class Solution {
return result;
}
};
*/

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> LIS(n, 1);

for (int i = n - 1; i >= 0; --i){
for (int j = i + 1; j < n; ++j){
if (nums[i] < nums[j])
LIS[i] = max(LIS[i], 1 + LIS[j]);
}
}

return *max_element(LIS.begin(), LIS.end());
}
};

0 comments on commit efda0eb

Please sign in to comment.