IPO | Easy Beginner Friendly | Story To Code | Leetcode 502 | codestorywithMIK
Вставка
- Опубліковано 26 сер 2024
- Whatsapp Community Link : www.whatsapp.c...
This is the 18th Video of our Playlist "Heap : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a good Heap problem : IPO | Easy Beginner Friendly | Story To Code | Leetcode 502 | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : IPO | Easy Beginner Friendly | Story To Code | Leetcode 502 | codestorywithMIK
Company Tags : MICROSOFT
My solutions on Github(C++ & JAVA) : github.com/MAZ...
Leetcode Link : leetcode.com/p...
My DP Concepts Playlist : • Roadmap for DP | How t...
My Graph Concepts Playlist : • Graph Concepts & Qns -...
My Recursion Concepts Playlist : • Introduction | Recursi...
My GitHub Repo for interview preparation : github.com/MAZ...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
The problem is to maximize the capital w after undertaking up to k projects, where each project has a specific profit and requires a certain amount of initial capital.
Steps:
Data Preparation:
Create a list of projects where each project is represented as a pair of capital and profit.
Sorting:
Sort the list of projects based on the required capital in ascending order. This allows us to efficiently access the projects that can be undertaken with the available capital.
Max Heap:
Use a max heap (priority queue) to keep track of the profits of the projects that can currently be undertaken (i.e., projects whose capital requirement is less than or equal to the current capital w).
Iterative Selection:
Iterate up to k times, in each iteration:
Add all projects that can be undertaken with the current capital to the max heap.
If the max heap is empty, break out of the loop as no more projects can be undertaken.
Otherwise, extract the maximum profit from the heap and add it to the current capital w.
This greedy approach ensures that at each step, the project with the maximum profit is chosen from the set of projects that can be undertaken, thus maximizing the capital efficiently.
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024
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 😍
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 UA-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
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.
today's POTD "Most Profit Assigning Work" is a similar problem.
Solved on my own!!.... Thank you for your efforts
Aapke explanation ke baad khudse code kar liya bhaiya❤❤
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.
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
Respected MIK Sir,
Thanks for explaining this Hard level problem in so much easy way.
You are a magician mik.
I can say now this is indeed a Medium level problem
Great Explanation!
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. 🙏
Very easy to understand! Great explanation 😇
This explanation is awesome 💯
Hoping to learn a lot from you
thanku bhiya for very good and clean explanation i am also doing this JUN Leetcode Challenge :)
i did upto the sorting part ut was unable to figure out how to use heap in this problem. Thanks for the solution.
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 UA-cam for this question. It would be great if you could take out some time and make a video on it. Thanks ! :)
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.
Very Lovely !!!
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
great explanation bhayia
Sir can this problem with dp like by using take skip concept..and return the maximum..
Thanks a lot bhaiya ❤❤
Most underrated channel. Why don't you advertise or post on social media to spread words about your channel.
Great explanation ✨
Best explaination!!
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
easiest hard question
Hey, this is great content, thanks for your efforts!
I wanted to ask, do you also upsolve contest questions?
One word - WOW 👌🏻
solved on my own just came her to other solution
k kuch cases me n se bada bhi diya hua tha
i know wo break wali condition se tackle ho rahi hai but for better understanding
k=min(k,n);
Goa me ek bade se W ke neeche ....
Bhai cheery pickup 1 pe video banao please 🥺🥺 not cherry pickup 2
what a great explaination!!
cant be make min heap acc to capital and using custom comparator , in case of tie , we keep high profit at top
Make vedio on Leetcode 315
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.
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
thanks a lot!!
what if in the question, w is decreased by the capital of the project?
how to figure out whether the problem should be solved by DP or greedy?
this question is similar to 0/1 Knapsack
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??
kya priority queue k bina bhi kar skte hein sorting me hi custom comparator use kar k ?
can we do it with min heap and a comparator function... if yes then someone pls explain how ???
you are legend!
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
❤❤
Hello MIK bhaia.
How to think greedy vs dp. like here why greedy worked??
Bhaiya C++ me DSA kru ya Java me please bta do bahut zyada confused hu...
this problem is similar to knapsack / house robber
correct me if i am wrong but, isn't this question leading to the concept of knapsack 0/1 ?
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.
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 🤔
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 we use tree set or some other data structure instead on heap/ PQ??
Damn 🫡🫡🙌
Great Explanation !!