forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_673.java
59 lines (51 loc) · 1.75 KB
/
_673.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.fishercoder.solutions;
/**
* 673. Number of Longest Increasing Subsequence
* Given an unsorted array of integers, find the number of longest increasing subsequence.
Example 1:
Input: [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
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.
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
*/
public class _673 {
public static class Solution1 {
/**
* Reference: https://discuss.leetcode.com/topic/103020/java-c-simple-dp-solution-with-explanation
*/
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
int[] cnt = new int[n];
int[] len = new int[n];
int max = 0;
int count = 0;
for (int i = 0; i < n; i++) {
len[i] = cnt[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
if (len[i] == len[j] + 1) {
cnt[i] += cnt[j];
}
if (len[i] < len[j] + 1) {
len[i] = len[j] + 1;
cnt[i] = cnt[j];
}
}
}
if (max == len[i]) {
count += cnt[i];
}
if (len[i] > max) {
max = len[i];
count = cnt[i];
}
}
return count;
}
}
}