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

КОМЕНТАРІ • 79

  • @aws_handles
    @aws_handles 2 місяці тому +17

    Those who don’t know,
    Try watching single threaded cpu video by MIK
    This will no longer be Hard
    Bawal teacher ho aap ❤❤❤

  • @manojitsaha6262
    @manojitsaha6262 2 місяці тому +11

    You are the best mentor for DSA bhaiya ❤❤❤❤ aj jitna bhi skilled hua hu DSA me apke liye...😊

  • @Abhay14
    @Abhay14 2 місяці тому +13

    What a great explanation bhaiya 😍

  • @bhargavinaik8145
    @bhargavinaik8145 2 місяці тому +4

    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.

  • @moupiya9286
    @moupiya9286 2 місяці тому +3

    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.

  • @jeehub041
    @jeehub041 2 місяці тому +2

    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.

  • @souravjoshi2293
    @souravjoshi2293 2 місяці тому +2

    today's POTD "Most Profit Assigning Work" is a similar problem.

  • @pokeindia5361
    @pokeindia5361 2 місяці тому +2

    Solved on my own!!.... Thank you for your efforts

  • @akshatsoni3183
    @akshatsoni3183 2 місяці тому +1

    Aapke explanation ke baad khudse code kar liya bhaiya❤❤

  • @masoomyf
    @masoomyf 2 місяці тому +1

    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.

  • @harshadakoranne475
    @harshadakoranne475 2 місяці тому +2

    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 !

    • @aws_handles
      @aws_handles 2 місяці тому +1

      Hey same for me. Single threaded cpu was awesome

  • @AbhijeetMuneshwar
    @AbhijeetMuneshwar 2 місяці тому +1

    Respected MIK Sir,
    Thanks for explaining this Hard level problem in so much easy way.

  • @Thriftinghai
    @Thriftinghai 2 місяці тому +3

    You are a magician mik.
    I can say now this is indeed a Medium level problem

  • @user-ym1nv1pw8i
    @user-ym1nv1pw8i Місяць тому +1

    Great Explanation!

  • @nikhilrathi3627
    @nikhilrathi3627 2 місяці тому +1

    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. 🙏

  • @monikavaid5083
    @monikavaid5083 2 місяці тому

    Very easy to understand! Great explanation 😇

  • @PriyanshuSingh-rm5xg
    @PriyanshuSingh-rm5xg 2 місяці тому +1

    This explanation is awesome 💯
    Hoping to learn a lot from you

  • @amango6297
    @amango6297 2 місяці тому +2

    thanku bhiya for very good and clean explanation i am also doing this JUN Leetcode Challenge :)

  • @pranavpranjal2717
    @pranavpranjal2717 2 місяці тому

    i did upto the sorting part ut was unable to figure out how to use heap in this problem. Thanks for the solution.

  • @heenamittal8621
    @heenamittal8621 2 місяці тому

    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 ! :)

  • @sauravchandra10
    @sauravchandra10 2 місяці тому

    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.

  • @anirudhyadav5082
    @anirudhyadav5082 2 місяці тому

    Very Lovely !!!

  • @11csepratikshaargulewar71
    @11csepratikshaargulewar71 2 місяці тому +1

    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

  • @pulkitsharma2304
    @pulkitsharma2304 2 місяці тому +1

    great explanation bhayia

  • @ZilaGhaziabad99
    @ZilaGhaziabad99 2 місяці тому +1

    Sir can this problem with dp like by using take skip concept..and return the maximum..

  • @gauravbanerjee2898
    @gauravbanerjee2898 2 місяці тому

    Thanks a lot bhaiya ❤❤

  • @gui-codes
    @gui-codes 2 місяці тому

    Most underrated channel. Why don't you advertise or post on social media to spread words about your channel.

  • @shashankpatil3132
    @shashankpatil3132 2 місяці тому +1

    Great explanation ✨

  • @Studykaro-ko8zn
    @Studykaro-ko8zn 2 місяці тому +1

    Best explaination!!

  • @focusman8801
    @focusman8801 2 місяці тому +1

    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

  • @pratik.784
    @pratik.784 2 місяці тому +1

    easiest hard question

  • @justlc7
    @justlc7 2 місяці тому

    Hey, this is great content, thanks for your efforts!
    I wanted to ask, do you also upsolve contest questions?

  • @ugcwithaddi
    @ugcwithaddi 2 місяці тому

    One word - WOW 👌🏻

  • @peterfromengland8663
    @peterfromengland8663 2 місяці тому

    solved on my own just came her to other solution

  • @S_O_EVERYTHING
    @S_O_EVERYTHING 2 місяці тому

    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);

  • @jevinkardani
    @jevinkardani 2 місяці тому

    Goa me ek bade se W ke neeche ....

  • @shubhtalk7073
    @shubhtalk7073 2 місяці тому

    Bhai cheery pickup 1 pe video banao please 🥺🥺 not cherry pickup 2

  • @sumitgupta310
    @sumitgupta310 2 місяці тому

    what a great explaination!!

  • @nkd575
    @nkd575 2 місяці тому

    cant be make min heap acc to capital and using custom comparator , in case of tie , we keep high profit at top

  • @nikeshsingh2081
    @nikeshsingh2081 2 місяці тому

    Make vedio on Leetcode 315

  • @sinnohperson8813
    @sinnohperson8813 2 місяці тому +1

    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?

  • @redox-7092
    @redox-7092 2 місяці тому

    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.

  • @PratikTripathy-yy4kz
    @PratikTripathy-yy4kz 2 місяці тому +1

    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?????

    • @jagritbudhiraja6391
      @jagritbudhiraja6391 2 місяці тому

      Haan toh bhai hum priority queue mei store karwa rahe Hai na toh wo apne aap max profit laakar dedega

  • @sonakshibajpai6445
    @sonakshibajpai6445 2 місяці тому

    thanks a lot!!

  • @namankumar2220
    @namankumar2220 2 місяці тому

    what if in the question, w is decreased by the capital of the project?

  • @floatingpoint7629
    @floatingpoint7629 2 місяці тому

    how to figure out whether the problem should be solved by DP or greedy?

  • @ayaanrashid960
    @ayaanrashid960 2 місяці тому

    this question is similar to 0/1 Knapsack

  • @shreyansh8789
    @shreyansh8789 2 місяці тому

    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??

  • @amirsuhail7126
    @amirsuhail7126 2 місяці тому

    kya priority queue k bina bhi kar skte hein sorting me hi custom comparator use kar k ?

  • @A_Myth963
    @A_Myth963 2 місяці тому

    can we do it with min heap and a comparator function... if yes then someone pls explain how ???

  • @thorodinson7259
    @thorodinson7259 2 місяці тому

    you are legend!

  • @samharsh1261
    @samharsh1261 2 місяці тому +1

    why dp solution does not work apart from constraints being tight?

    • @zebra-er6xc
      @zebra-er6xc 2 місяці тому

      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

  • @shabananoor9423
    @shabananoor9423 2 місяці тому

    ❤❤

  • @jays2144
    @jays2144 2 місяці тому +1

    Hello MIK bhaia.
    How to think greedy vs dp. like here why greedy worked??

  • @rit453
    @rit453 2 місяці тому

    Bhaiya C++ me DSA kru ya Java me please bta do bahut zyada confused hu...

  • @anjleshwasule2214
    @anjleshwasule2214 2 місяці тому +1

    this problem is similar to knapsack / house robber

  • @11csepratikshaargulewar71
    @11csepratikshaargulewar71 2 місяці тому +1

    correct me if i am wrong but, isn't this question leading to the concept of knapsack 0/1 ?

  • @AmandeepSingh-gv8gg
    @AmandeepSingh-gv8gg 2 місяці тому +1

    mik why not 0 1 knapsack intutuin?

    • @RohanPatil-gz6un
      @RohanPatil-gz6un 2 місяці тому

      because of extremely tight memory constarints. it will lead to mle

    • @RohitRaj-hl6ji
      @RohitRaj-hl6ji 2 місяці тому

      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.

  • @RohitRaj-hl6ji
    @RohitRaj-hl6ji 2 місяці тому

    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.

    • @gui-codes
      @gui-codes 2 місяці тому

      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

    • @RohitRaj-hl6ji
      @RohitRaj-hl6ji 2 місяці тому

      @@gui-codes yeah Yeh pehele yeh testcase output 27 de rha tha but phir 6 Dene lga tha. Maybe a glitch 🤔

  • @Ronakrewar
    @Ronakrewar 2 місяці тому

    why we are not iterating again from start in while(k--) loop.because all from start are less then w? please answer .

  • @aizad786iqbal
    @aizad786iqbal 2 місяці тому

    can we use tree set or some other data structure instead on heap/ PQ??

  • @user-ub2is4rs4x
    @user-ub2is4rs4x 2 місяці тому

    Damn 🫡🫡🙌

  • @priyanshkumar_iitd
    @priyanshkumar_iitd 2 місяці тому

    Great Explanation !!