Notes/Codes/Problem links under step 10 of A2Z DSA Course: takeuforward.o... Entire playlist: • Two Pointer and Slidin... Follow us on our other social media handles: linktr.ee/take...
Пікірлер: 95
@iam_bantu4 ай бұрын
striver bro please upload the greedy playlist asap .. ur lectures are literally dominating all dsa premium courses.. far more better than anyone
@raghavmanish242 ай бұрын
it's been already uploaded
@lonerider6695Ай бұрын
bantu bhai ko ram ram
@prasun1595Ай бұрын
dude from 2n to n , kudos to you cuz my mind aint gone beyond that!
@akworld27393 ай бұрын
lagta hai jindgi tutorial dekte dekte nikal jayigi
@evilgame61973 ай бұрын
kasam s ...sala karne betho to ye kese krenge...bas yahi chlta h
@anandunique14722 ай бұрын
sahi kaha bhaiya😂koi qn sliding window me slove nahi ho raha he
@amitmehta2285Ай бұрын
@@anandunique1472 wahi sabse jyda aata hain 🤣
@rishujeetrai5780Ай бұрын
mt dekho tutorial.. khud se code likho fir socho kya glt h. Key Tip: Dry Run your code with pen and paper. At last, kch smjh na aae to tutorial dekho approach smjhne k liye bs.
@Bhalu-kl7sqАй бұрын
@@rishujeetrai5780 mein to aise kr raha tha pehle video solution dekho then khud try karo
@kamalakannanng42065 ай бұрын
Striver - The G.O.A.T of DSA 💥
@user-nm2wc1tt9u2 ай бұрын
Optimal: class Solution { public: int characterReplacement(string s, int k) { int n = s.size(); int left = 0, right = 0, maxFreq = 0, maxLen = 0; int hash[26] = {0}; while(right maxFreq){ maxFreq = hash[s[right]-'A']; }; if((right-left+1)-maxFreq > k){ //trimming the left portion hash[s[left]-'A']--; left++; } maxLen = max(maxLen, right-left+1); right++; } return maxLen; } };
@beinginnitАй бұрын
Though this code runs gets accepted in LC, but i think something is missing: if((right-left+1)-maxFreq > k){ //trimming the left portion hash[s[left]-'A']--; left++; } what if my maxFreq was from i part and also contributing, here i need to recalculate the max freq. I may be wrong but this is what i think.
@nadellahridayesh3571Ай бұрын
@@beinginnit Yeah,I see that,but it neetcode channel also he explained in this way to optimize more to O(1) space like this,please have a look at it and explain
@paragroy53593 ай бұрын
Last 5 minutes are very precious, it is a must watch. That trick could be used in other questions as well for optimisation.
@rushidesai283613 күн бұрын
That he has been mentioning in his previous videos as well.
@madhvishrivastava3646Ай бұрын
Bro when you explain it becomes so easy to understand.. Thank you so much :)
@pailatejeswararao45722 ай бұрын
Thankyou very much man.I have watched 6 videos and solved this solution optimally by my own.I will learn everything from you 🙂
@kapilrules12 күн бұрын
Thank you brother. I understood it because of you !🙏
@az-zm4ji2 ай бұрын
First solution witout optimization: class Solution { public: int characterReplacement(string s, int k) { //FML int l = 0, r = 0, ans = 0, maxf = 0; unordered_map umap; while(r < s.length()){ char c = s[r]; if(umap.find(c) == umap.end()) umap.insert({c, 1}); else umap.find(c)->second++; maxf = max(maxf, umap.find(c)->second); while(r - l + 1 - maxf > k){ umap.find(s[l])->second--; for(auto x : umap){ maxf = max(maxf, x.second); } l++; } ans = max(ans, r - l + 1); r++; } return ans; } };
@nirmalgurjar81813 ай бұрын
Trick is we dont want to minimize our max freq variable to get better result, ie. if we get result for max freq 3 we will continue to look for max freq greater than 3 if it exists and ignore freq less than equal 3. This will eliminate need of freq map scan and also remove while loop if we want to look for better max length.
@user-oy1uy2is9x3 ай бұрын
thanks bhaiya,now i got it.
@davidkent57492 ай бұрын
Thanks a lot bro
@hashcodez7572 күн бұрын
#UNDERSTOOD
@ayaaniqbal35315 ай бұрын
After understanding the logic and without seeing the solution now i am able to code it myself. Thanks striver for making us understand this algorithm.😊
@kaushit3 ай бұрын
Ok let's understand how much you really know the depth. give me pattern of all substring in ABC string with k=1.
@shibainu75003 ай бұрын
@@kaushit {"A", "B", "C", "AB", "BC"} hi hoga na ?
@aloklaha869411 сағат бұрын
Thanks Striver. I was stuck on what will be the terminating condition for trimming down from left.
@raghavmanish242 ай бұрын
you are the legend for dsa ......thanks again striver
@stith_pragya4 ай бұрын
UNDERSTOOD....Thank You So Much for this wonderful video......🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@rajharsh37395 ай бұрын
Bhaiya I am very much Greedy for your Greedy Playlist So when you are uploading that ?? 🙌🙌🤑🤑
@tejasjaulkar96584 ай бұрын
yes we want greedy playlist bhaiyyaa
@dhruvmishra97814 ай бұрын
same bro plz give greedy playlist after this much needed
@paragroy53593 ай бұрын
keep on making such videos, thanks a lot Great Content
@sahilsukhdeve46952 ай бұрын
Java Code for java language forks class Solution { public int characterReplacement(String s, int k) { int n=s.length(); int maxlen=0,maxf=0; int i=0,j=0; Mapmap=new HashMap(); while(jk){ char ch2=s.charAt(i); map.put(ch2,map.get(ch2)-1); //decrement the frequency i++; } // if((j-i+1)+maxf
@Shivi32590Ай бұрын
understood
@adarshmv2614 ай бұрын
Please upload the code.. And looking forward for Heaps playlist please. Nowhere in youtube heaps problems are covered.. Please do it 🙏🏼🙏🏼🙏🏼🙏🏼 . It's being asked in interviews many times
@user-sz1rm7bj7r4 ай бұрын
Great bro !
@ShahNawaz-cx3pi2 ай бұрын
C++ Leetcode solution: class Solution { public: int characterReplacement(string s, int k) { int n = s.size(); int l = 0; int r = 0; int ans = 0; int hash[26]={0}; int maxFre = 0; while(rmaxFre){ maxFre = hash[s[r]-'A']; } if((r-l+1)-maxFre>k) { hash[s[l]-'A']--; l++; } ans = max(ans, r-l+1); r++; } return ans; } };
@ugthesep5706Ай бұрын
Did all the questions of sliding windows by myself with the optimal solutions. Just leaving the fruit in basket one. Watch the video to know all the possible ways of solving that particular problem ;> Easy Peasy Lemon Squeezy 😅
@dhruvdangi89232 ай бұрын
sir what a video this is just amazing 🙏🙌🙌🔻
@torishi823 ай бұрын
Awesome brother. Understood.
@oyeesharme15 күн бұрын
it was quite tough but understood bhaiya
@TARUNRAO-oe8bm2 ай бұрын
Striver bhaiya, I didnt understand why didnt we update the maxf when we are incrementing l. because the max trip will also change right when we change shorten the length of the subarray from front?
@sparksfly4421Ай бұрын
you're talking about his most optimal approach, i presume. In that case he didn't modify the maxfreq variable under the inner while loop because we don't need a maxfreq variable less than the current maxfreq variable regardless of the trimmed down substring we have right now. that's because, say, the current length of the substring is lesser but we still have the maxfreq value pertaining to the previous valid substring; in this case we simply would not require to have to check the length and change the consequent letters, we'd just ignore it. And to optimize it further, instead of looping through the letters till we get the valid window of (current_length - max_freq) > k, we could replace it with 'if' to check the condition only once because we can again simply ignore the additional trouble of having to loop through for each invalid checks which striver explains why in the other videos of his
@oyeesharme15 күн бұрын
@@sparksfly4421 thanks bhai
@sibiranganath3 күн бұрын
Understood
@sahilshinde37244 ай бұрын
You are writing it in very very small font can u please write which could be visible properly ! Thanks
@Ultra_InstinctBit.h2 ай бұрын
can we use use mapping for frequency calculations?
@dayashankarlakhotia49435 ай бұрын
public int characterReplacement(String s,int k){ int[]freq=new int[26]; char[]ans=s.toCharArray(); int n=ans.length,l=0,max=0; for(int r=0;r
@devgarg43314 ай бұрын
Bhaia will not the time complexity be N + 26(N) = 27N ??
@psinity2 ай бұрын
i agree
@KshitijTakarkhedeАй бұрын
Hey peeps , I didn't get "while loop removing to if" can you please elaborate ( it will help alot)
@tejaswaniguttula59614 ай бұрын
Hi anna....!!! I am super excited with your sliding window protocol series. I understood every video till now in this series except this video with the difference between only if condition and inner while loop and its inner for loop. Could you please elaborate it in depth with some specific examples. So, that I can understand the thought process behind it. Please please anna...😊😊😊
@nirmalgurjar81813 ай бұрын
Trick is we dont want to minimize our max freq variable to get better result, ie. if we get result for max freq 3 we will continue to look for max freq greater than 3 if it exists and ignore freq less than equal 3. This will eliminate need of freq map scan and also remove while loop if we want to look for better max length.
@Aryan-rb3yk2 ай бұрын
@@nirmalgurjar8181 aaah i get it now ,thanks i got the 26 for loop part , but not the inner while loop part
@shashankvashishtha4454Ай бұрын
O((n + n)) logic is failing on AABABBB, we cannot because of the while loop directly jump to a point where some ans is missed out, if we write if instead of while (like in logic 3) then it will work.
@mradulmaheshwari93534 ай бұрын
Even if you don't update the maxfreq in while loop of left then also it is working smoothly. Although thanks for wonderfull series striver . code - class Solution { public int characterReplacement(String s, int k) { int[] hash=new int[26]; int maxlen=Integer.MIN_VALUE; int maxfreq=Integer.MIN_VALUE; int l=0; int n=s.length(); for(int r=0;rk){ char cl=s.charAt(l); hash[cl-'A']--; l++; } maxlen=Math.max(maxlen,r-l+1); } return maxlen; } }
@fractionofinfinity8423 ай бұрын
Why would this work? Also once let's say maxfreq is 3 because of 'A', and then if we remove 'A' from the window how do we update maxfreq from 3 to 2 assuming no other char having freq 3?
@Dsa_kabaap3 ай бұрын
@@fractionofinfinity842actually we don't need to update the maxf value to the lesser value . Becz if u update it to lesser value like 2 , again it will come under > k while condition .
@Rahul_Mongia2 ай бұрын
use if (r-l+1-maxf>k) rather than while
@KartikeyTT2 ай бұрын
ty sir
@kaustubhagrawal66764 күн бұрын
too clean
@psinity2 ай бұрын
19:20 the time complexity should be O( N + N*26) right?
@sakshamsharma31562 ай бұрын
yes
@saketjaiswal93814 ай бұрын
hacker as always thanks bro
@vageshnp679211 күн бұрын
what data structure is used to store hashmap please anyone tell me?
@sachinkumarsharma648328 күн бұрын
class Solution { public: int characterReplacement(string s, int k) { int n = s.size(); int l=0,r = 0,maxf = 0,maxlen = 0; mapmpp; while(rk) { mpp[s[l]]--; l = l+1; } maxlen = max(maxlen,r-l+1); r++; } return maxlen; } };
@mansisethi81273 ай бұрын
I didn't understand , why we are doing maxf=0 in the else part. Can someone please explain ?
@Aryan-rb3yk2 ай бұрын
I guess he did it initially as we was going to check again for the max freq using that 0-26 for loop its not in else everything is in if I guess
@kiranchavan2632Ай бұрын
Where i can find sudo code?
@asad_iliyas2 ай бұрын
understood++
@everydaycodingwithgiyusama69493 ай бұрын
Is the naive or brute force code work properly for all test cases ? IF YES CAN ANYONE FIND ERROR IN MY CODE because It does not satisfy test case 2 of leetcode int[] arr = new int[26]; int maxc = 0; int maxl = 0; for(int i = 0; i< s.length();i++){ for(int j = i ; j < s.length() ; j++){ arr[s.charAt(j) - 'A']++; maxc = Math.max(maxc,arr[s.charAt(j)-'A']); if( (j - i + 1 ) - maxc
@ayushgakre94503 ай бұрын
int[] arr = new int[26]; should be after first loop bcz in next iteration is should be updated to 0
@shashankvashishtha4454Ай бұрын
int window3(string& s,int k){ //O(n) most optimal pure O(n) logic int i=0; int j=0; int len=0; int max_freq=0; vectorfreq(26,0); while(j < n){ freq[s[j] - 'A']++; max_freq = max(max_freq,freq[s[j] - 'A']); if((j-i+1) - max_freq > k){ freq[s[i]-'A']--; i++; // max_freq = 0;//use less //max_freq = *max_element(freq.begin(),freq.end());//it will make no sense to calculate max freq here in if part we will get max in next iteration with line: max_freq = max(max_freq,freq[s[j] - 'A']); }else{ len = max(len,j-i+1); } j++; } return len; } this is most optimal logic, max_freq = 0, logic not clear to me and it is even not working with while version of this function, but this one is intuitive and clear.
@ananttripathi61903 ай бұрын
why we are doing " -'A' " in hash[S[r]-'A' ????
@parahunter16503 ай бұрын
S[r]-'A' will give a integer value which we will use for indexing in our hash map For example if s[r]=B So, 'B'-'A'(i.e 66-65(ASCII VALUES OF B AND A) =1) So hash[1] will increase
@rayhangafoor42042 ай бұрын
ugh im not able to understand the maxFreq optimization logic
@Mohit_Q2 ай бұрын
28 June 2024 4:49pm
@aryankumar301824 күн бұрын
US
@tarunkr469829 күн бұрын
mera ho gya hai dimag kharab iss question se
@paarth_bhasin4 ай бұрын
where is the code in all 3 languages???
@lakshsinghania3 ай бұрын
do it yourself man
@priyanshubiswas93963 ай бұрын
@@lakshsinghania 😂😂
@user-gk4zy6bk7l3 ай бұрын
God
@shivsankarsuthar2 ай бұрын
bhai writing thoda shi se kro , smjh ni aata kya likha h, piche jake dekhna pdta h