[Java] Leetcode 673. Number of Longest Increasing Subsequence [DP Subsequence #7]

preview_player
Показать описание
In this video, I'm going to show you how to solve Leetcode 673. Number of Longest Increasing Subsequence which is related to DP Subsequence.

Here’s a quick rundown of what you’re about to learn:

⭐️ Course Contents ⭐️
⌨️ (0:00) Question
⌨️ (4:10) Solution Explain
⌨️ (13:43) Code

In the end, you’ll have a really good understanding on how to solve Leetcode 673. Number of Longest Increasing Subsequence and questions that are similar to this DP Subsequence.

Now, if you want to get good at DP Subsequence, please checkout my DP Subsequence playlist.

Рекомендации по теме
Комментарии
Автор

What is the purpose of line 25? cacheFreq is initialized as [1] and each index is only incremented so comparing max current value vs 1 would always be either 1 vs 1 or some higher value vs 1, correct?

DesignCell
Автор

Why its hard to get for the first time

Malayalam_learner
Автор

I found this implementation a bit difficult. It is difficult to count equal lengths of LISequences.
Another version:

class Solution {

public int findNumberOfLIS(int[] nums) {
int n = nums.length;
int[] LIS = new int[n];
int[] count = new int[n];

Arrays.fill(LIS, 1);

int maxLIS = Integer.MIN_VALUE;

for (int i = n-1; i >= 0; --i) {

int curMax = 0;
int curCount = 1;

for (int j = i+1; j < n; ++j) {

if (nums[j] > nums[i]) {

if (LIS[j] > curMax) {
curMax = LIS[j];
curCount = count[j];
} else if (LIS[j] == curMax) {
//dups produce many LISequences
//sum up all counters of dups
curCount+=count[j];
}

}
}

//dp
LIS[i] = 1 + curMax;
count[i] = Math.max(curCount, 1);

maxLIS = Math.max(maxLIS, LIS[i]);
}

int numberOfLIS = 0;
for (int i = 0; i < count.length; ++i) {
if (LIS[i] == maxLIS) {
numberOfLIS+=count[i];
}
}

return numberOfLIS;
}

}

sergeychepurnov