So, everyone asks to not put spoiler in titles, I put this on the basis of my experience with other leetcode tutorials of my time (eg: 3 years back, when i was preparing for internship, whenever i search for any leetcode problem, there used to come 1 channel's video most often, i'll not name it, though it was good, but as soon as there used to hard follow up of any problem or hard variant, he used to skip it & i hated that part everytime, that atleast write in beginning or tell you will teach very basic stuff) . Thus, i write in title or tell in beginning that what all you will get from video & look for that part accordingly. (Why DP, was very specific to me! I just thought, somehow DP was intuitive to me but later realised that it can't solve, so yeah discussed that too 🌚)
I am happy today, since i think this is the first time i came up with an algorithm on my own and the fact that i did not consider any existing template and stuff before coding the solution, and I was able to beat 100% with my solution
O(1) space approach : Suppose y > x in that case we have to remove first "ba" For that we can traverse the string with keeping count of b now whenever a occur & b's count > 0 we have found "ba" So increase the ans, decrease the count for b. Also we need to reset the b's count when any other char occur like bbbbx. Now then all the "ba" cases are done now we need to remove the "ab" cases, so for that we need all a that didn't make it in "ba". Basically all a for which there was no previous b to make a "ba" pattern, these all a can make ab pair so count all such a's and then increase the ans by min(cn(a), cn(b)) * min(x, y). This solution got accepted if anyone is looking for o(1) space approach
I rely wish your channel should grow because in todays world where other youtube channels as selling dreams this man is rely spreading real content and with huge amount of consistency
Hi Aryan, I have been learning DSA for a month, and I am solving daily challenges in a leetcode, I used to see your videos for understanding a problem bro, Thanks for your work. But my question is when I see a problem I cannot solve it by myself. It feels like I am not improving anything 😞I always look for a solution. Did you have the same feeling when you were learning DSA, How did you overcome this?
It is important to use pen and paper to properly understand the problems and dry run through the test cases to understand the nitty grittys of the problem. It will give you better insights to the problem and help to develop your own solution.
@@GalaxyStart62 yea bro, I lost all my confidence today after seeing this problem. But as you said I need to solve more problems to get muscle memory and problem solving skills. Hope I will get soon.
@@AnonymousAnonymous-ug8tp Yes bro I have been taking notes for every problem. First I will try to solve it without looking solution, But honestly I couldn't do it. I lost all my confidence today and messaged here. But I am not going to give up 🙂
sometimes i believe you exaggerate too much , that i loose my concentration , for me , it would be better if approach is point to point and concise. and you explain good no doubt.
So, everyone asks to not put spoiler in titles, I put this on the basis of my experience with other leetcode tutorials of my time (eg: 3 years back, when i was preparing for internship, whenever i search for any leetcode problem, there used to come 1 channel's video most often, i'll not name it, though it was good, but as soon as there used to hard follow up of any problem or hard variant, he used to skip it & i hated that part everytime, that atleast write in beginning or tell you will teach very basic stuff)
.
Thus, i write in title or tell in beginning that what all you will get from video & look for that part accordingly. (Why DP, was very specific to me! I just thought, somehow DP was intuitive to me but later realised that it can't solve, so yeah discussed that too 🌚)
THIS IS LITERALLY BY FAR THE BEST F**ING EXPLANATION THERE
WALKED THROUGH THE THOUGHT PROCESS FROM THE BEGINNER POV TO OPTIMIZATION , LOVED IT ❤
I am happy today, since i think this is the first time i came up with an algorithm on my own and the fact that i did not consider any existing template and stuff before coding the solution, and I was able to beat 100% with my solution
maqsad string 😁 this channel is the OG of daily challenge leetcode!
Superb explanation , Thanks
Thankyou so much bro amazing explanation💯
O(1) space approach :
Suppose y > x in that case we have to remove first "ba" For that we can traverse the string with keeping count of b now whenever a occur & b's count > 0 we have found "ba" So increase the ans, decrease the count for b. Also we need to reset the b's count when any other char occur like bbbbx.
Now then all the "ba" cases are done now we need to remove the "ab" cases, so for that we need all a that didn't make it in "ba". Basically all a for which there was no previous b to make a "ba" pattern, these all a can make ab pair so count all such a's and then increase the ans by min(cn(a), cn(b)) * min(x, y).
This solution got accepted if anyone is looking for o(1) space approach
I rely wish your channel should grow because in todays world where other youtube channels as selling dreams this man is rely spreading real content and with huge amount of consistency
Bro you are amazing 🙏
Thanks Sir. These types of videos help me with intuition and approach..
Naah, that maqsad string got me 💀
Thank you! It's interesting technique with modifying the string in place. Hmm
❤
this was my solution :
class Solution {
public:
int point(string &s,string target, int p){
int total=0;
string str="";
for(char c:s){
if(!str.empty() && string(1, str.back()) + c==target){
str.pop_back();
total+=p;
}
else{
str+=c;
}
}
s=str;
return total;
}
int maximumGain(string s, int x, int y) {
string s1="ab", s2="ba";
if(y>x){
swap(s1,s2);
swap(x,y);
}
return point(s,s1,x)+point(s,s2,y);
}
};
25:52 - Overwriting
Is the second approach some kind of pattern or is it only applicable to this question only?
please solve leetcode 913 cat and mouse
please solve leetcode 913 cat and mouse problem
Hi Aryan,
I have been learning DSA for a month, and I am solving daily challenges in a leetcode, I used to see your videos for understanding a problem bro, Thanks for your work. But my question is when I see a problem I cannot solve it by myself. It feels like I am not improving anything 😞I always look for a solution. Did you have the same feeling when you were learning DSA, How did you overcome this?
keep doing don't stop , it happens to all , once you have solved a good amount of problems like 200 + , it will come to your mind automatically.
It is important to use pen and paper to properly understand the problems and dry run through the test cases to understand the nitty grittys of the problem. It will give you better insights to the problem and help to develop your own solution.
@@GalaxyStart62 yea bro, I lost all my confidence today after seeing this problem. But as you said I need to solve more problems to get muscle memory and problem solving skills. Hope I will get soon.
@@AnonymousAnonymous-ug8tp Yes bro I have been taking notes for every problem. First I will try to solve it without looking solution, But honestly I couldn't do it. I lost all my confidence today and messaged here. But I am not going to give up 🙂
The catch is to do all concepts once then dpp or along with like after love or striver sheett
class Solution {
public int maximumGain(String s, int x, int y) {
Stack st = new Stack();
int ans =0;
for(int i =0 ; i < s.length() ; i++){
System.out.println(st);
if(s.charAt(i) == 'a'&& !st.isEmpty() && st.peek() == 'b' ){
if(y > x){
st.pop();
ans += y;
System.out.println(ans);
}else{
st.push(s.charAt(i));
}
}else if(s.charAt(i) == 'b' && !st.isEmpty() && st.peek() == 'a'){
if(y > x){
st.push(s.charAt(i));
}else{
st.pop();
ans += x;
System.out.println(ans + "x>");
}
}else{
st.push(s.charAt(i));
}
}
while(!st.isEmpty()){
char ch = st.pop();
if(!st.isEmpty() && st.peek() == 'a' && ch == 'b'){
ans += x;
}else if(!st.isEmpty() &&st.peek() == 'b' && ch == 'a'){
ans += y;
}
}
return ans;
}
}
Why is this solution wrong please anyone explain?
Just for algo
maqsad mt bhulna bhaijaan....😁😁
commenting "anything"
sometimes i believe you exaggerate too much , that i loose my concentration ,
for me , it would be better if approach is point to point and concise.
and you explain good no doubt.