For people who are asking why not DP? I had the same doubt I even tried to solve it with DP but could not pass all TCs, Got TLE, after going through some of the comments in the question I figured out. why dp is not suitable 1. The constraint is too high. 2. The local max will effect the global max so checking all possible choices is not needed. 3. The w is not reducing after picking any project. So if there was a project that we could have picked previously we will still be able to pick that project after finishing some other project. So we dont have to backtrack and check what would have happened for the next input if I dint do what I did earlier.
I think what you say at the beginning of your video, "medium/hard marked hain magar hum ise kafi easy bana denge ki aap khudse code kar pawoge," has some magical power in it. I've seen this happen many times. Even when I know which approach to use from intuition (and constraints), I just can't figure out how to apply them. After struggling for hours, I finally come to TH-cam to watch your video, and after hearing that, I think, "Okay, let's give it another try." Most of the time, during that attempt, I get some idea of how to apply the technique. After trying for some time, it just works.
brilliant explanation, so much easy... you did nothing but you did everything... ❤. Why I can't think, maybe because I want to hear a new story from you.
Sir literally aaj maza aa gaya iska concept samajh ke hum initially jab problem dekha the tab hum isko vector of pair of profit and cost banana ka soche and then usko sort karenge jyada profit ke basis pe and then traverse karke best answer nikal lenge. But aapne acha explain kia ki profit se pehle capital toh dekho aap usse bandhe hue ho so pehle is kam capital ke basis pe sort karo vector of pair ko. Thanks sir 🎉fot such nice mind blowing explanation.
I could solve this question because i saw your single threaded CPU question's explaination. Just some minor changes in that code!! Thankyou for teaching in such manner and building up our intution !
I think the heap operation in worst case (when all projects have same capital need as the current 'w') would be nlogn + klogn, nlogn for the push and then k times we need to pop and pick. Total = nlogn + nlogn + klogn = O(nlogn + klogn) If n>>k, O(nlogn) If k>>n, O(klogn)
Sorry bro offer lene ke badd mai tanda pad gaya 3 mahine baad aaj phir se shuru kar raha hu. ashirvad do ke mai consistent rahu. Aur ab next target product based. 🙏
Hello, Thanks for making this hard problem easy. 👏 Can you pls make a video explaination of Leetcode 768 as well, Good explainations are not available on TH-cam for this question. It would be great if you could take out some time and make a video on it. Thanks ! :)
for Time complexity what if k =4 and n = 50 and for all projects capital is 0 then pq will add all n elements in while loop so in this case can we consider time complexity of while loop with k as O(k*n * log n) ? please answer
Somehow this question seemed difficult. Was not able to get the intuition on how to solve this one and even after seeing the solution, was not able to code this on my own.
can we not sort the vector based on the first element of each inner vector in ascending order. However, when the first elements of two adjacent inner vectors are equal, we can sort them in descending order based on the second element using custom sort
I tried to combine the vectors in vectors of pairs and then sort it and find it greedily 23/35 The test case that was shown wrong had 111 n value. So I can't debug it How will you deal with those situations?
Hey , Thanks CodeStoryWith Mik ! Can You help me ? Actually mein approach soch leta hoon but code m implement nhi krpata kuch suggestion , Yeh wala question ki approach bhi soch li thi but code nhi ho rha tha.
Bhaiya jab ek baar koi profit use kar liya toh dubara use nhi kar sakte hai,but code meh hamne vector se woh element remove hee nhi kiya use karne ke baad, fir bhi code kaise chal raha hai error nhi aa raha??
But what will happen, when we have a case where capital[i]>profit[i], according to your solution, that will also be calculated, but that will minimize our total profit?????
I tried using the inclusion, exclusion principle but since index, number of projects and money there are 3 changing vairables, I think we need to use 3-D dp to implement this
can someone explain this testcase capital[] = {0,1,2} and profits[] = {1,2,3} w = 0 and k = 10. the output is 27 for this testcase. I am not able to figure out how this case is handled in the code.
because the w is purely increasing here but in knapsack we used to decrease our weight when we used to pick that weight element but here we are not decreasing the value of w. Also the constraints are much higher here.
Those who don’t know,
Try watching single threaded cpu video by MIK
This will no longer be Hard
Bawal teacher ho aap ❤❤❤
You are the best mentor for DSA bhaiya ❤❤❤❤ aj jitna bhi skilled hua hu DSA me apke liye...😊
What a great explanation bhaiya 😍
Best solution I have ever seen ❤❤❤❤❤
For people who are asking why not DP? I had the same doubt I even tried to solve it with DP but could not pass all TCs, Got TLE, after going through some of the comments in the question I figured out. why dp is not suitable
1. The constraint is too high.
2. The local max will effect the global max so checking all possible choices is not needed.
3. The w is not reducing after picking any project. So if there was a project that we could have picked previously we will still be able to pick that project after finishing some other project. So we dont have to backtrack and check what would have happened for the next input if I dint do what I did earlier.
I think what you say at the beginning of your video, "medium/hard marked hain magar hum ise kafi easy bana denge ki aap khudse code kar pawoge," has some magical power in it. I've seen this happen many times. Even when I know which approach to use from intuition (and constraints), I just can't figure out how to apply them. After struggling for hours, I finally come to TH-cam to watch your video, and after hearing that, I think, "Okay, let's give it another try." Most of the time, during that attempt, I get some idea of how to apply the technique. After trying for some time, it just works.
true
brilliant explanation, so much easy... you did nothing but you did everything... ❤. Why I can't think, maybe because I want to hear a new story from you.
Sir literally aaj maza aa gaya iska concept samajh ke hum initially jab problem dekha the tab hum isko vector of pair of profit and cost banana ka soche and then usko sort karenge jyada profit ke basis pe and then traverse karke best answer nikal lenge.
But aapne acha explain kia ki profit se pehle capital toh dekho aap usse bandhe hue ho so pehle is kam capital ke basis pe sort karo vector of pair ko.
Thanks sir 🎉fot such nice mind blowing explanation.
Solved on my own!!.... Thank you for your efforts
Aapke explanation ke baad khudse code kar liya bhaiya❤❤
I could solve this question because i saw your single threaded CPU question's explaination. Just some minor changes in that code!!
Thankyou for teaching in such manner and building up our intution !
Hey same for me. Single threaded cpu was awesome
Great Explanation!
Respected MIK Sir,
Thanks for explaining this Hard level problem in so much easy way.
This explanation is awesome 💯
Hoping to learn a lot from you
I think the heap operation in worst case (when all projects have same capital need as the current 'w') would be nlogn + klogn, nlogn for the push and then k times we need to pop and pick.
Total = nlogn + nlogn + klogn = O(nlogn + klogn)
If n>>k, O(nlogn)
If k>>n, O(klogn)
You are a magician mik.
I can say now this is indeed a Medium level problem
Very easy to understand! Great explanation 😇
thanku bhiya for very good and clean explanation i am also doing this JUN Leetcode Challenge :)
great explanation bhayia
Best explaination!!
Great explanation ✨
Very Lovely !!!
Most underrated channel. Why don't you advertise or post on social media to spread words about your channel.
Sorry bro offer lene ke badd mai tanda pad gaya 3 mahine baad aaj phir se shuru kar raha hu. ashirvad do ke mai consistent rahu. Aur ab next target product based. 🙏
Thanks a lot bhaiya ❤❤
what a great explaination!!
today's POTD "Most Profit Assigning Work" is a similar problem.
can you please share the code of the following
One word - WOW 👌🏻
Hello, Thanks for making this hard problem easy. 👏 Can you pls make a video explaination of Leetcode 768 as well, Good explainations are not available on TH-cam for this question. It would be great if you could take out some time and make a video on it. Thanks ! :)
Great Explanation !!
Hey, this is great content, thanks for your efforts!
I wanted to ask, do you also upsolve contest questions?
i did upto the sorting part ut was unable to figure out how to use heap in this problem. Thanks for the solution.
Sir can this problem with dp like by using take skip concept..and return the maximum..
for Time complexity
what if k =4 and n = 50 and for all projects capital is 0
then pq will add all n elements in while loop so in this case can we consider time complexity
of while loop with k as O(k*n * log n) ?
please answer
Somehow this question seemed difficult. Was not able to get the intuition on how to solve this one and even after seeing the solution, was not able to code this on my own.
thanks bhaiya for these wonderful solutions... Can you help me how your record good audio on ipad as I also want to start recording on my ipad?
can we not sort the vector based on the first element of each inner vector in ascending order. However, when the first elements of two adjacent inner vectors are equal, we can sort them in descending order based on the second element using custom sort
thanks a lot!!
I tried to combine the vectors in vectors of pairs and then sort it and find it greedily
23/35
The test case that was shown wrong had 111 n value. So I can't debug it
How will you deal with those situations?
you are legend!
Hey , Thanks CodeStoryWith Mik ! Can You help me ? Actually mein approach soch leta hoon but code m implement nhi krpata kuch suggestion , Yeh wala question ki approach bhi soch li thi but code nhi ho rha tha.
kya priority queue k bina bhi kar skte hein sorting me hi custom comparator use kar k ?
Bhaiya jab ek baar koi profit use kar liya toh dubara use nhi kar sakte hai,but code meh hamne vector se woh element remove hee nhi kiya use karne ke baad, fir bhi code kaise chal raha hai error nhi aa raha??
But what will happen, when we have a case where capital[i]>profit[i], according to your solution, that will also be calculated, but that will minimize our total profit?????
Haan toh bhai hum priority queue mei store karwa rahe Hai na toh wo apne aap max profit laakar dedega
cant be make min heap acc to capital and using custom comparator , in case of tie , we keep high profit at top
Bhaiya C++ me DSA kru ya Java me please bta do bahut zyada confused hu...
how to figure out whether the problem should be solved by DP or greedy?
Bhai cheery pickup 1 pe video banao please 🥺🥺 not cherry pickup 2
what if in the question, w is decreased by the capital of the project?
Make vedio on Leetcode 315
solved on my own just came her to other solution
why dp solution does not work apart from constraints being tight?
I tried using the inclusion, exclusion principle but since index, number of projects and money there are 3 changing vairables, I think we need to use 3-D dp to implement this
can we do it with min heap and a comparator function... if yes then someone pls explain how ???
easiest hard question
❤❤
Goa me ek bade se W ke neeche ....
why we are not iterating again from start in while(k--) loop.because all from start are less then w? please answer .
Because we need k distinct projects
can someone explain this testcase capital[] = {0,1,2} and profits[] = {1,2,3} w = 0 and k = 10. the output is 27 for this testcase. I am not able to figure out how this case is handled in the code.
output 6 hoga bhai.
k = 10 to hai but we only have 3 projects available and we will be able to do all of them with.
1 + 2 + 3 = 6
@@gui-codes yeah Yeh pehele yeh testcase output 27 de rha tha but phir 6 Dene lga tha. Maybe a glitch 🤔
mik why not 0 1 knapsack intutuin?
because of extremely tight memory constarints. it will lead to mle
because the w is purely increasing here but in knapsack we used to decrease our weight when we used to pick that weight element but here we are not decreasing the value of w. Also the constraints are much higher here.
this question is similar to 0/1 Knapsack
Hello MIK bhaia.
How to think greedy vs dp. like here why greedy worked??
correct me if i am wrong but, isn't this question leading to the concept of knapsack 0/1 ?
can we use tree set or some other data structure instead on heap/ PQ??
this problem is similar to knapsack / house robber
Damn 🫡🫡🙌