673. Number of Longest Increasing Subsequence

来源:互联网 发布:怎么做好网络金融销售 编辑:程序博客网 时间:2024/06/06 17:21

Given an unsorted array of integers, find the number of longest increasing subsequence.

Example 1:

Input: [1,3,5,4,7]Output: 2Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].

Example 2:

Input: [2,2,2,2,2]Output: 5Explanation: 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.

思路:DP,先求最大的递增连续序列的个数,然后再DP到i位置位置形成j个连续子序列的个数

package l673;class Solution {    public int findNumberOfLIS(int[] nums) {    if(nums.length == 0)return 0;        int max = 1;        int[] dp1 = new int[nums.length];        dp1[0] = 1;        for(int i=1; i<nums.length; i++) {        dp1[i] = 1;        for(int j=0; j<i; j++)        if(nums[i] > nums[j])        dp1[i] = Math.max(dp1[i], 1+dp1[j]);        max = Math.max(max, dp1[i]);        }                int[][] dp = new int[nums.length][1+max];        for(int i=0; i<nums.length; i++)dp[i][0]=1;        dp[0][1] = 1;        for(int i=1; i<nums.length; i++) {        for(int j=0; j<i; j++) {        dp[i][1] = 1;        for(int k=1; k<max; k++) {        if(nums[i] > nums[j])        dp[i][k+1] += dp[j][k];    }        }        }                int ret = 0;        for(int i=0; i<nums.length; i++)        ret += dp[i][max];        return ret;    }}


阅读全文
0 0
原创粉丝点击