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.
题目大意:给定一个未排序的int数组,找出最长子序列的个数
代码
package DP; /** * @Author OovEver * @Date 2017/12/15 10:06 */ public class LeetCode673 { public int findNumberOfLIS(int[] nums) { int n = nums.length; int res = 0; int max_len = 0; // 以nums[i]结尾的最长子序列的长度 int[] len = new int[n]; // 以nums[i]结尾的最长子序列的个数 int[] count = new int[n]; for(int i=0;i<n;i++) { len[i] = count[i] = 1; for(int j=0;j<i;j++) { if (nums[i] > nums[j]) { if(len[i]==len[j]+1) count[i] += count[j]; if (len[i] < len[j] + 1) { len[i] = len[j] + 1; count[i] = count[j]; } } } if(max_len==len[i]) res += count[i]; if (max_len < len[i]) { max_len = len[i]; res = count[i]; } } return res; } }