Number of Longest Increasing Subsequence
Thoughts
Code
class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length, max = 1, res = 0;
int[] f = new int[n], g = new int[n];
for (int i = 0; i < n; i++) {
f[i] = 1;
g[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
if (f[j] + 1 == f[i]) {
g[i] += g[j];
} else if (f[j] + 1 > f[i]) {
f[i] = f[j] + 1;
g[i] = g[j];
}
}
}
if (f[i] > max) {
max = f[i];
res = g[i];
} else if (f[i] == max) {
res += g[i];
}
}
return res;
}
}Analysis
Last updated