Number of Longest Increasing Subsequenc

class Solution {
public int findNumberOfLIS(int[] nums) {
		if (nums == null || nums.length == 0) return -1;
		int m = nums.length;
		int[] dp_len = new int[m];
		int[] dp_count = new int[m];
		Arrays.fill(counts, 1);

		dp_len[0] = 1;
		dp_count[0] = 1;
		
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < i; j++) {
			if (nums[i] > nums[j]) {
				if (dp_len[i] > dp_len[j]) {
					dp_len[i] = dp_len[j] + 1;
					dp_count[i] = dp_count[j];
				} else if (dp_len[i] == dp_len[j] + 1) {
					dp_count[i] += dp_count[j];
				}
			}
		}
	}

		int longest = 0;
		int ans = 0;
	
		for (int length : dp_len) {
			longest = Math.max(length, longest);
		}
		for (int i = 0; i < m; i++) {
			if (dp_len[i] == longest) {
				ans += dp_count[i];
			}
		}
		return ans;
	}
}

最后更新于

这有帮助吗?