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;
}
}
网友评论