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));
}
}
Comments | NOTHING