Number of Longest Increasing Subsequence - Dynamic Programming - Leetcode 673 - Python

preview_player
Показать описание


0:00 - Read the problem
2:04 - Intuition of LIS
3:43 - Explain Recursive Solution
7:31 - Explain DP Solution
15:07 - Coding DP Solution

leetcode 673

#dynamic #programming #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

This should definitely be a hard question! 😂

shenzheng
Автор

Whenever I can't follow official editorial, I go for Neetcode. You're superb in explain algorithm thought flow, and more importantly, you code according!

fxrcode
Автор

I dont do a leetcode question if there is no NeetCode video for it LOL

farazahmed
Автор

Bro, you're the best, and funny 😂 1000 bucks 😂 Keep it up 💪🏻

iknoorsingh
Автор

OMG YES NEETCODE!! Thank you for doing solution! Really appreciate it!

johns
Автор

nice explaination and and way you update maxLen and maxCnt in 1 loop, never though of that because I afraid something wrong and not clear, so I do 2 seperated loops.

gothien
Автор

I get so happy when I see your knew vids 😊

irarose
Автор

There is a O(n logn) solution for this using patience sort but it is considerably trickier to code. Also, you can store a tuple, instead of a list as dp's value.

andrepinto
Автор

Excellent Explanation Sir Please Keep Going

ankurbhambri
Автор

How will look the solution using BIT or segment tree?

rasecbadguy
Автор

Java code for this -
class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
int dp_len[] = new int[n]; //store length of LIS starting at each index
int dp_count[] = new int[n]; //store count of such LIS of length starting and each index.
Arrays.fill(dp_len, 1);
Arrays.fill(dp_count, 1);
int lenLIS = 0; //length of longest LIS
int totalCount = 0; //total no. of longest LIS

for(int i = n - 1; i >= 0; i --) { //start from right to left
int curMax = 1;
int curCount = 1;

for(int j = i + 1; j < n; j ++) { //for each ele, check if LIS exists on right
if(nums[j] > nums[i]) { //increasing order
int len = dp_len[j];
int count = dp_count[j];
if(len + 1 > curMax) {
curMax = len + 1;
curCount = count;
} else if(len + 1 == curMax) {
curCount += count;
}
}
}

if(curMax > lenLIS) {
lenLIS = curMax;
totalCount = curCount;
} else if(curMax == lenLIS) {
totalCount += curCount;
}
dp_len[i] = curMax;
dp_count[i] = curCount;
}
return totalCount;

}
}

divyagracie
Автор

I hope you’ll be doing Advent of Code solutions in a few days!

bgfreakn
Автор

can u make video for 332 Reconstruct Itinerary

yashsrivastava
Автор

You github link is not working anymore

melissac
Автор

Wonderful explanation. I’m kind of new to problem solving. What’s a cache?

messironaldo
Автор

I was doing the same ques !
but I solved it :) but your explanation makes it crystal clear !

Shanky_
Автор

couldn’t we solve this in O(n) time? by using the closing window approach and keeping track of the max length and count in a map, then increment that count in the map every time another LIS is found (while we are iterating the array)

juniperspice
Автор

Best explanation!! Thank you so much for this!

arushiarora
Автор

please make a telegram group where we can ask doubt

anupambiswas
Автор

I love your videos and they are super super helpful!!! However, sometimes I do find your explanations just a little on the wordy side.. the best explanation still though!

pizzahotdog