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
Approach 2 is op❤
Thank you so much ❤️🙏
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😄
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
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.
You made my day.
Thank you so much ❤️❤️🙏😇
No one can beat the explanation of this channel.
I watched this video till R2L logic and paused started to code I was able do I myself. Thanks again.😍
That’s absolutely what I want. Just understand the logic and intuition and then try the code on your own.
Thank you for sharing ❤️🙏😇
One of the best explanations of 2nd approach I found since morning. Most of the channels have not discussed approach-2 . You nailed it.
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.
Glad it helped! 🙏❤️😇
only video I found clearly explained 0(1) solution. Thank you so much. those dry runs were so helpful
Glad it helped! 😇🙏
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!!
Thank you 🙏❤️😇
2nd approach reminds me of counting valleys question from hackerrank. But i would never have thought to apply it in this question
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.
@@codestorywithMIKcan u give link of that valley Question??
u are really talented but very underrated bhai❣
Thank You Sir keep uploading the first approach was like trapping rain water
Indeed.
Thank you 😇🙏❤️
Shandhar Explanation👌👌👌👌
Thanks a lot 😊
2nd approach is best your explanation style is excellent 👌 🎉❤
Thanks a lot 😊 ❤️
thanks for the o(1) solution bhaiya... i could'nt find it anywhere else...
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
Great Explanation
Glad it was helpful! 🙏
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
Approach 2 was more intuitive to me .
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.
hidden gem
Best Explanation! 👍🏻
awesome dude :) great approach
Thanks a lot
Thank You :)
You're welcome! 🙏😇
With your solution the hard question also become easy
Thank you 😇❤️
2nd explanation is really good . Thanks 👍
what an explanation! excellent!
Thank you so much 😊
Great Explanation 👌🏻
Thank you 😊
Please make a solution video on Expression add operator leetcode question, No one on TH-cam explained very well , i am waiting please
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.
Great explanation as always!!👌👏
Thank you 🙏😇
Bhaiya weekly contest ka videos bhi jaldi lao
Will soon plan. ❤️❤️❤️
Bohot time dry run krne ke baad finally samaj aaya!
So glad to hear that ❤️👌
Thanku bhaiya ❤
Thank you 😊
is it normal if I am not getting full intuition? I have solved around 6-7 greedy problems
Tagdaa Sir ji.....
Thank you 😇❤️
Well explained bhaiya
Thank you 🙏❤️
thankyou bhaiya
Thank you 😇❤️
God!
🙏🙏🙏❤️❤️❤️
Thanku sir!
Glad I could help ♥️♥️
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
It seems to be working fine for me. All test cases are passing for me.
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.
Confusing explanation on the part where there is a dip.
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
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;
}
};
2nd approach wasn't clearly explained and caused a lot of confusion
Apologies if there were confusion.
Would you kindly point where exactly i lost you ?
I’ll try my best to explain here ❤️🙏
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
Sure ❤️
Feedback taken
@@codestorywithMIK ❤ love your videos
bhaiya leetcode ke question ki aur github ki link kal wale question ki daal di hai apne
... please fix it
Thanks.
Let me fix it
Done. Thank you 🙏❤️
❤❤
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]
Thank you 🙏😇