int f(int ind,int prev,vector<int>& nums,vector<vector<int>>& dp)
{
if(ind<0)
return 0;
if(dp[ind][prev]!=-1)
return dp[ind][prev];
int nonpick,pick;
nonpick = f(ind-1,prev,nums,dp);
pick=0;
if(nums[ind]<nums[prev])
pick = 1+f(ind-1,ind,nums,dp);
return dp[ind][prev] = max(pick,nonpick);
}
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
int p = n-1;
vector<vector<int>>dp(n+1,vector<int>(n+1,-1));
nums.push_back(INT_MAX);
return f(n-1,n,nums,dp);
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter