class Solution {
public:
int longest(vector<int>& nums, int i){
// a helper function. Returns the longest ascending sub sequence that ENDS at position i
if (i==0) return 1;
else{
if (nums[i] > nums[i-1]) return longest(nums, i-1) + 1;
else return 1;
}
}
int lengthOfLIS(vector<int>& nums) {
vector<int> res;
for (int i = 0; i < nums.size(); i++)
res.push_back(longest(nums, i));
int global_max = 0;
for (int j = 0; j < res.size(); j++){
if (res[j] > global_max)
global_max = res[j];
}
return global_max;
}
};
A wrong version of code. The longest helper function has an error. Note there should not be "else return 1;" as not necessarily starts a new array.
In LeetCode it's different. The array is not necessarily continuous.
In LeetCode it's different. The array is not necessarily continuous.
1 Response
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.