forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_673.java
40 lines (37 loc) · 1.18 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
package com.fishercoder.solutions;
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;
}
}
}