TypechoJoeTheme

IT技术分享

统计

[LeetCode 673] Number of Longest Increasing Subsequence [Java]

2017-10-16
/
0 评论
/
729 阅读
/
正在检测是否收录...
10/16

1. Description

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

2. Runtime Distribution

3. Submission Details

4. Example

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


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.

5. Code

public class LeetCode0673 {

    public int findNumberOfLIS(int[] nums) {

        if (nums == null || nums.length == 0) {
            return 0;
        }

        int len[] = new int[nums.length + 1];
        int count[] = new int[nums.length + 1];

        int maxLen = 0;
        int result = 0;

        for (int i = 0; i < nums.length; 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];
                    } else if (len[i] < len[j] + 1) {
                        len[i] = len[j] + 1;
                        count[i] = count[j];
                    }
                }
            }
            if (maxLen == len[i]) {
                result += count[i];
            } else if (maxLen < len[i]) {
                maxLen = len[i];
                result = count[i];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        LeetCode0673 leetcode = new LeetCode0673();
        int[] nums = new int[] { 2, 2, 2, 2, 2 };
        System.out.println(leetcode.findNumberOfLIS(nums));
    }

}
DP
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/1145/(转载时请注明本文出处及文章链接)