leetcode673+最長子序列的個數,dp
阿新 • • 發佈:2018-11-01
https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/
class Solution { public: int findNumberOfLIS(vector<int>& nums) { vector<int> dp(nums.size(), 1); vector<int> ct(nums.size(), 1); int maxLen = 1; for(int i=1; i<nums.size(); i++){ for(int j=0; j<i; j++){ if(nums[i]>nums[j]){ if(dp[i] < dp[j]+1){ dp[i] = dp[j]+1; ct[i] = ct[j]; } else if (dp[i]==dp[j]+1){ ct[i] += ct[j]; } } } maxLen = max(maxLen, dp[i]); } int ret = 0; for(int i=0; i<ct.size(); i++) if(dp[i]==maxLen) ret+=ct[i]; return ret; } };