[LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence.
Example 1:
Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: [2,2,2,2,2] Output: 5 Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
Java:
public int findNumberOfLIS(int[] nums) { int n = nums.length, res = 0, max_len = 0; int[] len = new int[n], cnt = new int[n]; for(int i = 0; i<n; i++){ len[i] = cnt[i] = 1; for(int j = 0; j <i ; j++){ if(nums[i] > nums[j]){ if(len[i] == len[j] + 1)cnt[i] += cnt[j]; if(len[i] < len[j] + 1){ len[i] = len[j] + 1; cnt[i] = cnt[j]; } } } if(max_len == len[i])res += cnt[i]; if(max_len < len[i]){ max_len = len[i]; res = cnt[i]; } } return res; }
Python:
class Solution(object): def findNumberOfLIS(self, nums): """ :type nums: List[int] :rtype: int """ # Time: O(n^2) # Space: O(n) dp, longest = [[1, 1] for i in range(len(nums))], 1 for i, num in enumerate(nums): curr_longest, count = 1, 0 for j in range(i): if nums[j] < num: curr_longest = max(curr_longest, dp[j][0] + 1) for j in range(i): if dp[j][0] == curr_longest - 1 and nums[j] < num: count += dp[j][1] dp[i] = [curr_longest, max(count, dp[i][1])] longest = max(curr_longest, longest) return sum([item[1] for item in dp if item[0] == longest])
Python:
class Solution(object): def findNumberOfLIS(self, nums): """ :type nums: List[int] :rtype: int """ dp = [[1, 1] for i in range(len(nums))] max_for_all = 1 for i, num in enumerate(nums): max_len, count = 1, 0 for j in range(i): if nums[j] < num: if dp[j][0] + 1 > max_len: max_len = dp[j][0] + 1 count = 0 if dp[j][0] == max_len - 1: count += dp[j][1] dp[i] = [max_len, max(count, dp[i][1])] max_for_all = max(max_len, max_for_all) return sum([item[1] for item in dp if item[0] == max_for_all])
Python: wo
class Solution(object): def findNumberOfLIS(self, nums): """ :type nums: List[int] :rtype: int """ if not nums: return 0 n = len(nums) res = 0 cnt = [1] * n mx = [1] * n max_len = 1 for i in xrange(n): cur_longest = 1 for j in xrange(i): temp = 1 if nums[i] > nums[j]: temp = mx[j] + 1 if cur_longest < temp: cur_longest = temp mx[i] = cur_longest cnt[i] = cnt[j] elif cur_longest == temp: cnt[i] += cnt[j] if mx[i] > max_len: max_len = mx[i] res = cnt[i] elif mx[i] == max_len: res += cnt[i] return res
C++:
int findNumberOfLIS(vector<int>& nums) { int n = nums.size(), res = 0, max_len = 0; vector<pair<int,int>> dp(n,{1,1}); //dp[i]: {length, number of LIS which ends with nums[i]} for(int i = 0; i<n; i++){ for(int j = 0; j <i ; j++){ if(nums[i] > nums[j]){ if(dp[i].first == dp[j].first + 1)dp[i].second += dp[j].second; if(dp[i].first < dp[j].first + 1)dp[i] = {dp[j].first + 1, dp[j].second}; } } if(max_len == dp[i].first)res += dp[i].second; if(max_len < dp[i].first){ max_len = dp[i].first; res = dp[i].second; } } return res; }
C++:
class Solution { public: int findNumberOfLIS(vector<int>& nums) { int res = 0, mx = 0, n = nums.size(); vector<int> len(n, 1), cnt(n, 1); for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { if (nums[i] <= nums[j]) continue; if (len[i] == len[j] + 1) cnt[i] += cnt[j]; else if (len[i] < len[j] + 1) { len[i] = len[j] + 1; cnt[i] = cnt[j]; } } if (mx == len[i]) res += cnt[i]; else if (mx < len[i]) { mx = len[i]; res = cnt[i]; } } return res; } };
C++:
class Solution { public: int findNumberOfLIS(vector<int>& nums) { int res = 0, mx = 0, n = nums.size(); vector<int> len(n, 1), cnt(n, 1); for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { if (nums[i] <= nums[j]) continue; if (len[i] == len[j] + 1) cnt[i] += cnt[j]; else if (len[i] < len[j] + 1) { len[i] = len[j] + 1; cnt[i] = cnt[j]; } } mx = max(mx, len[i]); } for (int i = 0; i < n; ++i) { if (mx == len[i]) res += cnt[i]; } return res; } };
类似题目:
[LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
[LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列
All LeetCode Questions List 题目汇总