Candy distribution problem

Поділитися
Вставка
  • Опубліковано 10 лют 2025
  • This video contains a very important problem on candy distribution. We need to find the minimum number of candies required for distribution among children. This is a problem from leetcode. The code link is given below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    CODE LINK: leetcode.com/p...

КОМЕНТАРІ • 141

  • @techdose4u
    @techdose4u  Рік тому

    🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037
    🔴 𝐂𝐡𝐞𝐜𝐤𝐨𝐮𝐭 𝐚𝐥𝐥 𝐨𝐮𝐫 𝐂𝐨𝐮𝐫𝐬𝐞𝐬: techdose.co.in/

  • @swapnilingle4435
    @swapnilingle4435 3 роки тому +52

    My issue with this solution is, it makes sense only after you know the solution.
    However, if you don't know the answer and are trying to devise a solution, in no way will it occur to you to check only left child and then only right childs and then merge.
    The intuition looking forward does NOT make sense, looking backwards sure it works.

    • @ayushisharma5883
      @ayushisharma5883 2 роки тому +8

      totally agree. I always try to come up to the solution or atleast as closest to it as possible. But qustns like this are very difficult to come up with intutuion.

    • @aashishkumarsingh4664
      @aashishkumarsingh4664 2 роки тому +2

      1,2,88,88,88,2,1 if you check for these tests case

    • @Rahul_246a
      @Rahul_246a 2 роки тому +3

      You just need to practice more and more questions to come up with these kinds of approaches. I came up with this same approach on my own.

    • @Iceman0291
      @Iceman0291 2 роки тому +4

      Ye badi achi baat ki aap ne. :)

    • @gtbaba123
      @gtbaba123 2 роки тому

      True he dont know solution.

  • @TechiiEngineer
    @TechiiEngineer 3 роки тому +39

    hard level problem seems easy level after watching your explanations

    • @techdose4u
      @techdose4u  3 роки тому +3

      🤗

    • @piyush814
      @piyush814 3 роки тому +2

      Because it is not the most optimized solution. There is O(n) time and O(1) space solution as well.

    • @ramkrushnamadole874
      @ramkrushnamadole874 2 роки тому

      Indeed...

  • @pinigantikrishnavamsi5131
    @pinigantikrishnavamsi5131 4 роки тому +4

    Thank u for providing the Logic . I wrote python code for this problem
    n=int(input())
    a=[]
    for _ in range(n):
    a.append(int(input()))
    l=[1]*n
    r=[1]*n
    c=0
    for i in range(1,n): #left comparison#
    if(a[i-1]

  • @nightmare_9
    @nightmare_9 3 роки тому +14

    Another great explanation, solved one more important problem because of you. Thanks buddy !!

  • @shivalikagupta3433
    @shivalikagupta3433 2 роки тому +5

    I had tried both the initial ways which you instructed - sorting and larger , smaller. Even I also made an array comparing left, but then I thought right is left uncovered, it is bound to give incorrect answer and this is where i stumped. Actually it was just a little ahead, doing same with right and then max. however it didn't occur in my mind. Nicely explained thanks.

    • @pryansh_
      @pryansh_ Рік тому +1

      no problem shivalika ! you seem to be a good girl, it happens sometimes.

  • @aakashyadav6228
    @aakashyadav6228 4 роки тому +8

    This man is a legend.

  • @rosonerri-faithful
    @rosonerri-faithful 3 роки тому +6

    Thanks for the explanation Surya Sir! You genius Sir!!!!💫

  • @nathanjeong
    @nathanjeong 3 роки тому +3

    Your videos have the best explanations! subscribed for sure

  • @bhishmagaming6917
    @bhishmagaming6917 7 місяців тому

    great dude , loved the beautiful explanation

  • @alfredoosorio2599
    @alfredoosorio2599 Рік тому

    Actually there is a O(N Log K) solution where K is the number of distinct ratings it was very easy to come up with that solution you just need to save the original indexes and sort by rating, process the ratings starting from the smallest to the largest. However the optimal solution is O(N) runtime and O(1) space.

  • @nquanta1548
    @nquanta1548 2 роки тому

    Nice explanation 👍👍👍

  • @surajbaranwal56.
    @surajbaranwal56. 2 роки тому +1

    Well explanation sir, thanks, keep posting.

  • @coder-bz2ch
    @coder-bz2ch 2 роки тому

    Yes Great explanation

  • @MP-ny3ep
    @MP-ny3ep Рік тому

    Perfect explanation. Thank you !!!

  • @abhishekjain383
    @abhishekjain383 3 роки тому +1

    Thank You TechDose. You explained this problem very nicely.

  • @jhonrobaon1669
    @jhonrobaon1669 3 роки тому +1

    Brother, your videos are great. Can you please let me know what software are you using for creating videos (screen record and drawing on screen)?
    Thanks

  • @ranjith6177
    @ranjith6177 Рік тому

    Nice explanation bro and thanks.but total candidate 15=3+2+1+2+3+2+1+2

  • @asishkumar9432
    @asishkumar9432 3 роки тому +5

    can't we do without using another array while R2L and make changes in first L2R array? (just to reduce space complexity)
    Great explanation...Thank you!!!

  • @MohitSinha4
    @MohitSinha4 3 роки тому +2

    Thank you!!!!
    You are the best!

  • @saikirank6357
    @saikirank6357 2 роки тому

    The Best explanation ever

  • @avikmallick2493
    @avikmallick2493 3 роки тому +1

    Love u and ur explanations brother..u r a life saver

  • @rajattalnikar5565
    @rajattalnikar5565 5 років тому +13

    Based on your explanation I have written the C++ Solution for the Problem.
    class Solution {
    public:
    int candy(vector& ratings) {
    int n=ratings.size();
    int L2R[n];
    int R2L[n];

    for(int i=0;i=0;i--)
    {
    if(ratings[i]>ratings[i+1])
    R2L[i]=R2L[i+1]+1;
    }

    int res[n];

    for(int i=0;i

    • @GodOfFaith
      @GodOfFaith Рік тому

      do not believe leetcode on this rating

  • @srilekha9177
    @srilekha9177 5 років тому +3

    Thanks for uploading this video. Clear explanation. If possible, please make a video on "Egg Dropping Puzzle" and "Coin Change Problem".

  • @aikanshpriyam3977
    @aikanshpriyam3977 5 років тому +4

    Great Explanation sir, Thank you

  • @nicky1652
    @nicky1652 Рік тому

    I realised that ascending and descending order cases can be solved without using the Math MAX method, We don't need to do anything on the left2right side loop but on the right2left we can instead use a simple check to max value if else I say!.
    Something like below😅
    // right 2 left side loop
    for(int i= ratings.length - 2; i >= 0; i--){ // {1,3,4,5,2};
    if(ratings[i] > ratings[i + 1]){
    if(minCandyRatings[i] > minCandyRatings[i + 1]){
    continue;
    }else{
    minCandyRatings[i] = minCandyRatings[i + 1] + 1;
    }
    }
    }
    😅

  • @hrishitsarkar6388
    @hrishitsarkar6388 2 роки тому

    great explanation buddy

  • @abhishekbabbar9234
    @abhishekbabbar9234 4 роки тому +10

    you told us how t0 solve the problem....but we want to know how to think the solution...how does it comes to mind that this question has to be done this way?

    • @vamsia5543
      @vamsia5543 4 роки тому +2

      it only comes with practice bro !
      Keep praticing on such tags where u feel it difficult

    • @techdose4u
      @techdose4u  4 роки тому +5

      Yes you are correct. Do a lot of practice and things will start appearing before you just seeing the question ;)

    • @abhishekbabbar9234
      @abhishekbabbar9234 4 роки тому

      Thanks 😊

    • @ajitkumarsingh871
      @ajitkumarsingh871 4 роки тому

      What tags are for this problem ?

    • @AbhishekKumar-yv6ih
      @AbhishekKumar-yv6ih 4 роки тому

      @@ajitkumarsingh871 greedy

  • @vaibhavagarwal3757
    @vaibhavagarwal3757 2 роки тому

    Please also explain the most optimized approach of this problem.

  • @manthankhorwal1477
    @manthankhorwal1477 4 роки тому +1

    I did this with sorting ... That solution was easy to come up with

  • @raviashwin1157
    @raviashwin1157 3 роки тому +1

    God level teaching🙏

  • @masc0648
    @masc0648 2 роки тому

    Fantastic explanation thank you.

  • @yashrastogi3726
    @yashrastogi3726 4 роки тому +1

    Very nice explanation

  • @shivangagrawal2923
    @shivangagrawal2923 2 роки тому

    Awesome Solution

  • @RahulKumar-jl4kz
    @RahulKumar-jl4kz 4 роки тому +2

    thanks for such a doubtless explanation

  • @surbhitamrakar1638
    @surbhitamrakar1638 3 роки тому +2

    Very well explained!

  • @roopeshkummara3519
    @roopeshkummara3519 4 роки тому

    can you explain how if 1st case has 6 candies and 2nd case has 8 candies how do we get 8 candies(14:27)?You told we need to get max(left,right)+1,then in that case we need to get 8+1 candies?

  • @samyakjain720
    @samyakjain720 3 роки тому +1

    Love you bro!!

  • @akashdoppalapudi3548
    @akashdoppalapudi3548 3 роки тому +1

    Thank you. Great explaination

  • @MohitSharma-rz1uq
    @MohitSharma-rz1uq 2 роки тому

    perfect explanation

  • @abhicasm9237
    @abhicasm9237 2 роки тому +1

    Can someone explain me why do we need to take the max ?
    When moving from right to left, we can update the left array values to be greater than the right side values. But it doesn't seem to work.
    It works for this example too that Tech Dose gave, but when I submit it, it gave wrong ans.
    The code ->
    public int candy(int[] A) {
    int n = A.length;
    int[] candy = new int[n];
    Arrays.fill(candy, 1);
    // left check
    for(int i = 1; i< n; i++) {
    if(A[i] > A[i-1]) candy[i] = candy[i-1] + 1;
    }

    //right check
    for(int i = n-2; i>=0; i--) {
    if(A[i] > A[i+1]) candy[i] = candy[i+1] +1;
    }

    int ans = 0;
    for(int num: candy) {
    ans+= num;
    }

    return ans;
    }
    Please explain me someone.

  • @sssumeet
    @sssumeet 2 роки тому

    great🙌

  • @manjunathp9466
    @manjunathp9466 3 роки тому

    Here is the Python code
    class Solution:
    def candy(self, ratings: List[int]) -> int:
    left_list=[1 for i in range(len(ratings))]
    right_list=[1 for i in range(len(ratings))]
    for i in range(1,len(ratings)):
    if ratings[i]>ratings[i-1]:
    left_list[i]=left_list[i-1]+1
    for i in range(len(ratings)-2,-1,-1):
    if ratings[i]>ratings[i+1]:
    right_list[i]=right_list[i+1]+1
    total=0
    for i in range(len(ratings)):
    total+=max(left_list[i],right_list[i])
    return total

  • @ninadylan4649
    @ninadylan4649 4 роки тому +1

    Thanku..you explained so well!!

  • @srinivasaraob4168
    @srinivasaraob4168 4 роки тому +1

    34 also has to get 3 candy's when the rating is same...

  • @yitingg7942
    @yitingg7942 4 роки тому +1

    Excellent explanation sir! Can you please also do one video for 621. Task Scheduler? Highly appreciate it!

  • @ShreyaSingh-vr9qi
    @ShreyaSingh-vr9qi 4 роки тому +2

    Nice explaination!!

  • @waqaskhan2165
    @waqaskhan2165 5 місяців тому

    hi all i need one suggestion . i find very hard to understnad the leetcode problem then i come here and just undrsntadn the logic and do the code my self i dont copy code from any where i just take help to understand the alogorithm is it normal or i should not do so ?

  • @Uniyaltech
    @Uniyaltech 2 роки тому +1

    How to think like that?

  • @rahulsinghai3033
    @rahulsinghai3033 4 роки тому

    How to solve these type of problem
    You are given N red pieces of candy and M blue pieces of candy. Write a program to find the number of arrangements of candy in which not more than K pieces of the same color are placed next to each other

  • @arthamsreenivas8858
    @arthamsreenivas8858 4 роки тому +1

    Great explanation

  • @csmadhav
    @csmadhav 4 роки тому +1

    good explaination

  • @arnavkarforma3015
    @arnavkarforma3015 4 роки тому +1

    Do you use tab to create these videos or touch screen laptop.

    • @techdose4u
      @techdose4u  4 роки тому +1

      Tab

    • @arnavkarforma3015
      @arnavkarforma3015 4 роки тому

      @@techdose4u thanks for the reply, just wondering how do you move the cursor on the tab, is it some app?

    • @arnavkarforma3015
      @arnavkarforma3015 4 роки тому

      BTW I am a fan of your contents.. you have one of the best explanations videos on Ds & Algo.

  • @ankitraj359
    @ankitraj359 3 роки тому +1

    Can you provide explanation for 0(1) space solution

  • @RahulSharma-cp1dn
    @RahulSharma-cp1dn 4 роки тому +2

    Thanks for the explanation, But few of the test cases are yet not cleared in Hacker rank, Can you please suggest any edit

    • @techdose4u
      @techdose4u  4 роки тому +1

      Did you try this on leetcode as well ? I think there might be a little variation somewhere (even possibly on constraints). Please compare with leetcode version.

    • @RahulSharma-cp1dn
      @RahulSharma-cp1dn 4 роки тому +1

      @@techdose4u Thank You.. i was not including the extreme elemets while scanning right to left.. All cool now. Thanks

    • @techdose4u
      @techdose4u  4 роки тому

      @@RahulSharma-cp1dn nice :)

  • @ankitaverma2271
    @ankitaverma2271 4 роки тому +1

    Thankyou Tech Dose

  • @utubecom9185
    @utubecom9185 5 років тому +2

    If we take 12 4 3
    Then we need only 3 candies
    And with this solution we need 6 candies???

    • @techdose4u
      @techdose4u  5 років тому +9

      We need 6 candies. 1 candy is guaranteed for everyone. After that, 4 is a nbr of 3 and greater than 3. So, 4 should have atleast 1 candy greater than 3. So, at 4, we will have 2 candies. After that since 12 is adjacent to 4 and greater than 4, therefore, 12 should have atleast 1 candy greater than 4. So, it has 3 candies (2+1). So, total candies reqd = 6. I hope you should concentrate on problem statement 😅

  • @siddharthsingh9409
    @siddharthsingh9409 5 років тому +1

    nicely done

  • @SharanyaVRaju
    @SharanyaVRaju 4 роки тому +1

    I don't get the max(left, right) concept.

    • @techdose4u
      @techdose4u  4 роки тому

      Because of calculation dependency on neighbours.

    • @blasttrash
      @blasttrash 4 роки тому +2

      Here is another way to look at it. If we take min or just the first value or the second value etc, then it might satisfy *only one condition* (that is left neighbor or right neighbor) depending on circumstances.
      However if we take max, we are *100% sure* that it will guarantee both neighbor conditions.

  • @chavanitinsaichowdary8088
    @chavanitinsaichowdary8088 2 роки тому

    I think it will not for the case of sorted array

  • @dishahayaran6074
    @dishahayaran6074 3 роки тому

    Nice explanation but answer would be 17.

  • @dhanashreegodase4445
    @dhanashreegodase4445 3 роки тому

    Thank You

  • @ellagicham
    @ellagicham 4 роки тому +1

    can we do it in single pass through the array?

    • @techdose4u
      @techdose4u  4 роки тому

      I dint try. The only important thing to kkow is info about left and right traversal. Then it can be done. Try it.

    • @blasttrash
      @blasttrash 4 роки тому

      check the leetcode solution editorial, it can be done with single array therefore it has O(n) space.
      And there is an even better solution( which takes no space at all) which uses some slope formula etc, but I didn't understand their leetcode's explanation.

  • @bonprog
    @bonprog Рік тому

    how to make it with one array?

  • @shubhampatil386
    @shubhampatil386 2 роки тому

    This problem can also be solved in one pass nd o(1) space.

  • @ajitkumarsingh871
    @ajitkumarsingh871 4 роки тому +2

    Is this a problem to be solved from dynamic programming ?

  • @sarthaksarangi
    @sarthaksarangi Рік тому

    Java Solution accordiung to the explanation
    public class Solution {
    public int candy(int[] A) {
    int [] prefixSum = new int [A.length];
    prefixSum[0] =1;
    for(int i =1; i< A.length; i++){
    if(A[i] > A[i-1]){
    prefixSum[i] = prefixSum[i-1] +1;
    }
    else prefixSum[i] =1;
    }
    int [] suffixSum = new int [A.length];
    suffixSum[A.length -1] =1;
    for(int i =A.length -2; i>=0 ; i--){
    if(A[i] > A[i+1]){
    suffixSum[i] = suffixSum[i+1] +1;
    }
    else suffixSum[i] =1;
    }
    int ans =0;
    for(int i=0; i< A.length; i++){
    ans += Math.max(prefixSum[i], suffixSum[i]);
    }
    return ans;
    }
    }

  • @yashsrivastava677
    @yashsrivastava677 3 роки тому

    where is the code?

  • @srividhyaparthasarathy5401
    @srividhyaparthasarathy5401 4 роки тому +1

    Can you give us the code sir...im getting runtime error

    • @techdose4u
      @techdose4u  4 роки тому +1

      I don't have the code now. I cleaned my setup long back and used different account on leetcode which is closed now 😅 Please see the solution from discussions.

    • @srividhyaparthasarathy5401
      @srividhyaparthasarathy5401 4 роки тому +1

      @@techdose4uit's ok thanks sir

    • @techdose4u
      @techdose4u  4 роки тому +1

      Welcome :)

    • @shresthmishra9329
      @shresthmishra9329 4 роки тому

      int Solution::candy(vector &A) {
      int n = A.size();
      vector left(n,1);
      vector right(n,1);
      for(int i = 0; iA[i+1]){
      right[i] = right[i+1]+1;
      }
      }
      int ans = 0;
      for(int i = 0; i< n; i++){
      ans += max(left[i],right[i]);
      }
      return ans;
      }

  • @emilyhuang2759
    @emilyhuang2759 3 роки тому

    How does this make sense? What is the proof of concept? How does it show that minimum number of candies needed with all possible order of combination?

  • @shashikantkumar5095
    @shashikantkumar5095 4 роки тому +1

    epic!!

  • @tankuharshida2855
    @tankuharshida2855 3 роки тому

    For the love of God Ćlement stop I can't I even memorize the dialogues your gf says "do you wanna be SDE at Google 😀"

  • @spoiler407
    @spoiler407 2 роки тому

    Shashank Naku telusu man

  • @nathann4291
    @nathann4291 Рік тому

    boring

  • @sarthakgupta9858
    @sarthakgupta9858 2 роки тому

    great explanation