Q: https://leetcode.com/problems/number-of-longest-increasing-subsequence/
class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
int[] length = new int[n];
int[] count = new int[n];
Arrays.fill(length, 1);
Arrays.fill(count, 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
if (length[j] + 1 > length[i]) {
length[i] = length[j] + 1;
count[i] = 0;
}
if (length[j] + 1 == length[i]) {
count[i] += count[j];
}
}
}
}
int maxLength = 0;
int result = 0;
for (int len : length) {
maxLength = Math.max(maxLength, len);
}
for (int i = 0; i < n; i++) {
if (length[i] == maxLength) {
result += count[i];
}
}
return result;
}
}
Q: https://leetcode.com/problems/target-sum/description/
class Solution {
public int findTargetSumWays(int[] nums, int s) {
int sum = 0;
for (int n : nums)
sum += n;
return sum < s || (s + sum) % 2 > 0 ? 0 : subsetSum(nums, (s + sum) >>> 1);
}
public int subsetSum(int[] nums, int s) {
int[] dp = new int[s + 1];
dp[0] = 1;
for (int n : nums)
for (int i = s; i >= n; i--)
dp[i] += dp[i - n];
return dp[s];
}