美文网首页
Number of Longest Increasing Sub

Number of Longest Increasing Sub

作者: Frank_Kivi | 来源:发表于2018-06-27 15:20 被阅读7次

https://www.lintcode.com/problem/number-of-longest-increasing-subsequence/description

public class Solution {
    /**
     * @param nums: an array
     * @return: the number of longest increasing subsequence
     */
    public int findNumberOfLIS(int[] nums) {
        // Write your code here
        Node[] nodes = new Node[nums.length];
        int max = 0;
        for (int i = 0; i < nodes.length; i++) {
            nodes[i] = new Node();
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    if (nodes[j].length + 1 > nodes[i].length) {
                        nodes[i].length = nodes[j].length + 1;
                        nodes[i].count = nodes[j].count;
                    } else if (nodes[j].length + 1 == nodes[i].length) {
                        nodes[i].count += nodes[j].count;
                    }
                }
            }
            max = Math.max(nodes[i].length, max);
        }
        int result = 0;
        for (int i = 0; i < nodes.length; i++) {
            Node node = nodes[i];
            if (node.length == max) {
                result += node.count;
            }
        }
        return result;
    }

    private class Node {
        int length = 1;
        int count = 1;
    }
}

相关文章

网友评论

      本文标题:Number of Longest Increasing Sub

      本文链接:https://www.haomeiwen.com/subject/fcobyftx.html