Number of Longest Increasing Subsequence LeetCode solution in Java
1class Solution {2public int findNumberOfLIS(int[] nums) {3int N = nums.length;4int[] dp = new int[N];5int[] count = new int[N];6Arrays.fill(dp, 1);7Arrays.fill(count, 1);8int maxi = 1;9for (int i = 0; i < N; i++) {10for (int j = 0; j < i; j++) {11if (nums[j] < nums[i] && dp[j] + 1 > dp[i]) {12dp[i] = dp[j] + 1;13// inherit a new one14count[i] = count[j];15maxi = Math.max(dp[i], maxi);16} else if (nums[j] < nums[i] && dp[j] + 1 == dp[i]) {17// got one as same len, increase count18count[i] += count[j];19}20}21} // for ends22int maxlis = 0;23for (int i = 0; i < N; i++) {24if (maxi == dp[i]) {25maxlis += count[i];26}27}28return maxlis;29}30}