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
| class Solution { public int findNumberOfLIS(int[] nums) {
int[] f = new int[nums.length];
int[] g = new int[nums.length];
int maxLength = 1; for (int i = 0; i < nums.length; i++) { f[i] = g[i] = 1;
for (int j = 0; j < i; j++) { if (nums[j] < nums[i]) { if (f[j] + 1 > f[i]) { f[i] = f[j] + 1; g[i] = g[j]; } else if (f[j] + 1 == f[i]) { g[i] += g[j]; } } }
maxLength = Math.max(maxLength, f[i]); }
int count = 0; for (int i = 0; i < f.length; i++) { if (f[i] == maxLength) { count += g[i]; } } return count; } }
|
References
673. Number of Longest Increasing Subsequence