CANDY | 2 Approaches | O(1) Space | O(N) Space | AMAZON | Leetcode - 135

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • This is the 18th Video on our Greedy Playlist.
    In this video we will try to solve a very good Greedy Problem - CANDY (Leetcode-135).
    Trust me, this will no longer be a Hard Problem.
    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.
    Problem Name : CANDY
    Company Tags : AMAZON
    My solutions on Github : 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 GitHub Repo for interview preparation : github.com/MAZ...
    Subscribe to my channel : / @codestorywithmik
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    #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

ความคิดเห็น • 89

  • @sachinmohanty4577
    @sachinmohanty4577 ปีที่แล้ว +23

    Approach 2 is op❤

  • @siddharth.chandani
    @siddharth.chandani 5 หลายเดือนก่อน +7

    Whenever I searched for a question & found your video then I feel so happy that this man will definitely explained me solution... Thanks a lot for your brilliant explanation as always😄

  • @wearevacationuncoverers
    @wearevacationuncoverers ปีที่แล้ว +1

    Man you are just on another level. I have literally never seen such kind of in-depth explanation in any channel yet. And that too free

  • @itsaayush7357
    @itsaayush7357 7 หลายเดือนก่อน +1

    M dam sure no one can beat mik in explaination of code.
    He makes u understand like u r a small kid learning from your elders.
    Thank you bro, for these clear content and your efforts.

    • @codestorywithMIK
      @codestorywithMIK  7 หลายเดือนก่อน

      You made my day.
      Thank you so much ❤️❤️🙏😇

  • @Momentsofmagic28
    @Momentsofmagic28 ปีที่แล้ว +2

    No one can beat the explanation of this channel.

  • @avnish-wf8rv
    @avnish-wf8rv ปีที่แล้ว +1

    I watched this video till R2L logic and paused started to code I was able do I myself. Thanks again.😍

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว

      That’s absolutely what I want. Just understand the logic and intuition and then try the code on your own.
      Thank you for sharing ❤️🙏😇

  • @AlishaKhan-ww3io
    @AlishaKhan-ww3io ปีที่แล้ว

    One of the best explanations of 2nd approach I found since morning. Most of the channels have not discussed approach-2 . You nailed it.

  • @obulpathi2729
    @obulpathi2729 9 หลายเดือนก่อน +1

    Thank you for sharing the 2nd approach, the leet code's official solution made it very complicated to understand, but you made it very simple.

    • @codestorywithMIK
      @codestorywithMIK  9 หลายเดือนก่อน

      Glad it helped! 🙏❤️😇

  • @mahinhossen4251
    @mahinhossen4251 7 หลายเดือนก่อน +1

    only video I found clearly explained 0(1) solution. Thank you so much. those dry runs were so helpful

  • @aadil4236
    @aadil4236 ปีที่แล้ว +1

    Bhai kya intuition tha. Mene ye solve karliya tha, lekin vo intuition ni aa raha tha. Right lag raha tha muje lekin 100% intuition ni aa raha tha ke kyu ye kaam karna chaahiye. Kya samjaya he bhai. Aapki explain karne ki style bohot achhi he!!

  • @trailsnail
    @trailsnail ปีที่แล้ว +3

    2nd approach reminds me of counting valleys question from hackerrank. But i would never have thought to apply it in this question

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +5

      Absolutely. It reminded me about the valley qn. I also remember that the Valley qn was asked in OA of swiggy once some months back.

    • @pokeindia5361
      @pokeindia5361 3 หลายเดือนก่อน

      ​@@codestorywithMIKcan u give link of that valley Question??

  • @sarveshjoshi6913
    @sarveshjoshi6913 2 หลายเดือนก่อน +2

    u are really talented but very underrated bhai❣

  • @yikes3807
    @yikes3807 ปีที่แล้ว +2

    Thank You Sir keep uploading the first approach was like trapping rain water

  • @balreddya6604
    @balreddya6604 8 หลายเดือนก่อน +1

    Shandhar Explanation👌👌👌👌

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 ปีที่แล้ว +2

    2nd approach is best your explanation style is excellent 👌 🎉❤

  • @mailatkaushal
    @mailatkaushal 10 หลายเดือนก่อน +1

    thanks for the o(1) solution bhaiya... i could'nt find it anywhere else...

  • @surajsidar2294
    @surajsidar2294 5 หลายเดือนก่อน

    The following is not passing the test cases (approach 2)
    What can be the reason?
    class Solution {
    public int candy(int[] ratings) {
    int n= ratings.length;
    int i=1;
    int candy =0;
    while(iratings[i-1])
    {
    peak++;
    i++;
    candy+=peak;
    if(i==n)
    return candy;
    }
    //dip
    int dip=0;
    while(i

  • @shoaibakhtar9194
    @shoaibakhtar9194 8 หลายเดือนก่อน +1

    Great Explanation

  • @Abhay14
    @Abhay14 ปีที่แล้ว +1

    bhaiya kl ki video me aap explain krna ki aap question ko approach kaise krte ho,
    because maine jbb minimum dekha to maine socha isme recursion hogga buy ye greedy se ho rha h, pls tell your mindset while solving a new problem

  • @iamnottech8918
    @iamnottech8918 หลายเดือนก่อน

    Approach 2 was more intuitive to me .

  • @souravjoshi2293
    @souravjoshi2293 ปีที่แล้ว

    I am always relieved now that whenever there is a problem I am stuck in, there will be MIK who will make it easy for me.

  • @iamnottech8918
    @iamnottech8918 หลายเดือนก่อน +2

    hidden gem

  • @TheBusaSinger
    @TheBusaSinger ปีที่แล้ว +1

    Best Explanation! 👍🏻

  • @aliksarkar2935
    @aliksarkar2935 6 หลายเดือนก่อน +1

    awesome dude :) great approach

  • @tutuimam3381
    @tutuimam3381 ปีที่แล้ว +2

    Thanks a lot

  • @AmarjeetKumar-to9ub
    @AmarjeetKumar-to9ub ปีที่แล้ว +2

    Thank You :)

  • @kashifrahman
    @kashifrahman ปีที่แล้ว +1

    With your solution the hard question also become easy

  • @shabananoor9423
    @shabananoor9423 ปีที่แล้ว

    2nd explanation is really good . Thanks 👍

  • @herculean6748
    @herculean6748 ปีที่แล้ว +1

    what an explanation! excellent!

  • @anuppatankar4294
    @anuppatankar4294 ปีที่แล้ว +2

    Great Explanation 👌🏻

  • @motivationkiaag1601
    @motivationkiaag1601 ปีที่แล้ว

    Please make a solution video on Expression add operator leetcode question, No one on TH-cam explained very well , i am waiting please

  • @sanjaykatta6499
    @sanjaykatta6499 11 หลายเดือนก่อน

    Bro, on an average what is the amount of time you spend before arriving on a correct solution for a problem. Because sometimes it takes hour and more for me and still end up seeing your video for solution.

  • @sayandip8041
    @sayandip8041 ปีที่แล้ว +1

    Great explanation as always!!👌👏

  • @anuppal3243
    @anuppal3243 ปีที่แล้ว +1

    Bhaiya weekly contest ka videos bhi jaldi lao

  • @sauravchandra10
    @sauravchandra10 ปีที่แล้ว +1

    Bohot time dry run krne ke baad finally samaj aaya!

  • @umeshbisht1054
    @umeshbisht1054 ปีที่แล้ว +1

    Thanku bhaiya ❤

  • @SanjeevKumar-xv5lw
    @SanjeevKumar-xv5lw 3 หลายเดือนก่อน

    is it normal if I am not getting full intuition? I have solved around 6-7 greedy problems

  • @prashantvishwakarma6100
    @prashantvishwakarma6100 ปีที่แล้ว +1

    Tagdaa Sir ji.....

  • @oqant0424
    @oqant0424 ปีที่แล้ว +1

    Well explained bhaiya

  • @Vishal-ds6ly
    @Vishal-ds6ly ปีที่แล้ว +2

    thankyou bhaiya

  • @samannwaysil4412
    @samannwaysil4412 ปีที่แล้ว +1

    God!

  • @mdsharique2426
    @mdsharique2426 ปีที่แล้ว +1

    Thanku sir!

  • @ujjawaltyagi8540
    @ujjawaltyagi8540 ปีที่แล้ว +1

    the 2nd approach is not working anymore its giving error for test case : [1,3,4,5,2] output: 10 , expection
    code
    class Solution {
    public:
    int candy(vector& r) {
    int n = r.size();
    int i=1;
    int candy = n;
    while(ir[i-1])
    {
    peak++;
    candy+=peak;
    i++;
    if(i==n)
    return candy;
    }

    int dip = 0;
    while(i

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +1

      It seems to be working fine for me. All test cases are passing for me.

    • @ShrishDollin
      @ShrishDollin ปีที่แล้ว +3

      second while loop se return mt kro, as told, after every cycle/mountain completed, extras substract krne hai. pehle hi return kiya isiliye 12 aa raha hai output instead of 11.

  • @abhimanyuambastha2595
    @abhimanyuambastha2595 3 หลายเดือนก่อน

    Confusing explanation on the part where there is a dip.

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 ปีที่แล้ว +1

    Public class solution {
    Public int Candy(int []ratings){
    int n=ratings. length;
    int up count =0,down count =0,peak count=0,candy=1;
    for(int i=1;i

    • @dayashankarlakhotia4943
      @dayashankarlakhotia4943 ปีที่แล้ว +1

      class solution {
      public int Candy(int []ratings){
      int n=ratings. length;
      int[]candies =new int [n];
      Array. fill (candies,1);
      for(int i=1;iratings [i-1]){
      candies [i]=candies [i-1]+1;
      }
      }
      int count =candies [n-1];
      for(int i=n-2;i>=0;i--){
      if(ratings [i]>ratings [i+1]){
      candies [i]=MATH.MAX (candies [i],candies [i+1]+1);
      }
      count +=candies [i];
      }
      return count;
      }
      };

  • @ridj41
    @ridj41 7 หลายเดือนก่อน +1

    2nd approach wasn't clearly explained and caused a lot of confusion

    • @codestorywithMIK
      @codestorywithMIK  7 หลายเดือนก่อน

      Apologies if there were confusion.
      Would you kindly point where exactly i lost you ?
      I’ll try my best to explain here ❤️🙏

  • @cupcake2298
    @cupcake2298 2 หลายเดือนก่อน

    bhai aap ek line thoda zada baar hi repeat karte ho. matlab I know ki ache se samajhne me help hoti hai, but esa try karo ki pehle ache se form karlo sentence which is crisp and clear. usse video ki length bhi thodi kam hogi and aapke hi views badhenge, and viewers ko bhi help hogi. Thanks

    • @codestorywithMIK
      @codestorywithMIK  2 หลายเดือนก่อน

      Sure ❤️
      Feedback taken

    • @cupcake2298
      @cupcake2298 2 หลายเดือนก่อน

      @@codestorywithMIK ❤ love your videos

  • @prakharverma8524
    @prakharverma8524 ปีที่แล้ว +1

    bhaiya leetcode ke question ki aur github ki link kal wale question ki daal di hai apne
    ... please fix it

  • @codeandtalk6
    @codeandtalk6 ปีที่แล้ว +1

    ❤❤

  • @floatingpoint7629
    @floatingpoint7629 ปีที่แล้ว +1

    thanks for the explanation. incase any is looking for bruteforce solution:
    public class Solution {
    public int candy(int[] ratings) {
    int[] candies = new int[ratings.length];
    Arrays.fill(candies, 1);
    boolean hasChanged = true;
    while (hasChanged) {
    hasChanged = false;
    for (int i = 0; i < ratings.length; i++) {
    if (i != ratings.length - 1 && ratings[i] > ratings[i + 1] && candies[i] 0 && ratings[i] > ratings[i - 1] && candies[i]