leetcode 300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.Example:
Input:
Output: 4
Explanation: The longest increasing subsequence is , therefore the length is 4.
Note:
There may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
dp solution
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
if(nums.size() == 0) return 0;
int dp = {0};
dp = 1;
int max_ans = 0;
for(int i = 0; i < nums.size();i++){
int max1 = 0;
for(int j = 0; j < i; j++){
if(nums < nums){
max1 = max(max1, dp);
}
}
dp = max1 +1;
max_ans = max(max_ans,dp);
}
return max_ans;
}
};
页:
[1]