Тёмный

Pick Toys | An Interesting Sliding Window Problem 

Aditya Verma
Подписаться 251 тыс.
Просмотров 90 тыс.
50% 1

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 250   
@TheAdityaVerma
@TheAdityaVerma 3 года назад
Like this comment if your were able to reduce it into the previous problem by your own. (I want to get a count of class toppers :P)
@harmankhurana7702
@harmankhurana7702 3 года назад
Shouldn't it be: Number of unique characters "less than or equal to" 2? PS: Really Appreciate your work. Too Good!!
@adeshpandey6989
@adeshpandey6989 3 года назад
@@harmankhurana7702 No Herman if you choose unique character less than 2 you can always add at least one more character to increase the size of subarray without breaking second condition coz at a given position there can't be 2 characters Ex Aaaaabc If you selecting 5 a with one unique char then you can always include 5a and 1 b which is larger subarray
@harmankhurana7702
@harmankhurana7702 3 года назад
@@adeshpandey6989 but if the array is “aaaaa” i.e with only one type of toy, the answer will be 0 (if unique chars==2) and 5 (if unique chars
@neilchaudhary007
@neilchaudhary007 3 года назад
Yeah this problem is equivalent to longest substring with atmost 2 d distinct items/toys, i got this intuition while you were explaining the problem statement only thanks for this series loved it, please i messaged you like 5 months back on LinkedIn please if you take out some time to reply that will be great.
@neilchaudhary007
@neilchaudhary007 3 года назад
@@harmankhurana7702 yeah you are Right this problem is equivalent to longest substring with Atmost 2 distinct items/toys
@amanvijayvargiya3468
@amanvijayvargiya3468 3 года назад
Now I am placed in good company with good package after some rejections.For me you are more than a teacher and I know shukriya to kam hi padega bro but still thanks uuuuuuuuuuuuuuuh soooooooo much 😁😁😁😁😁😊and kabhi waqat mile to Nit Jalandhar me hiring ke liye jarur aana😅😅..Thanks again!!
@gauravmandal007
@gauravmandal007 2 года назад
congratulations
@dared2sing904
@dared2sing904 2 года назад
@aman vijayvargiya Kaunsi company??
@aesthetic_vibes_404
@aesthetic_vibes_404 2 года назад
@@dared2sing904 10 din mai peas double wali company Founder vipasha Bashu
@ayushyadav8665
@ayushyadav8665 Год назад
@@aesthetic_vibes_404 he's my super senior at NIT J.. He was placed in Delhivery !
@gauravraj2604
@gauravraj2604 3 года назад
mummy ji ki conditions: 1. line s uthao --> mtlb substring 2. max k types --> max k unique characters in the substring
@nitinverma_121
@nitinverma_121 3 года назад
This question is just longest substring with at most k unique characters
@BalakrishnaPerala
@BalakrishnaPerala 4 месяца назад
I just lately discovered his channel, longest substring with at most k unique characters == longest substring with k unique characters. because number of toys types atmost k will never be greater than number of k type toys. for example : aaaabc let k=2 , here o/p = 5, we try to take atmost (k=2) means all substrings with hm.size()
@kundann_n9989
@kundann_n9989 2 месяца назад
@@BalakrishnaPerala na it wont work if there are only one type of toys
@BalakrishnaPerala
@BalakrishnaPerala 2 месяца назад
@@kundann_n9989 yes, we need to add little small condition, if(maxLength == 0) return str.length(); whole code : import java.util.HashMap; public class Solution { public static int kDistinctChars(int k, String str) { // Write your code here int ml = 0 ; int count = 0; int n = str.length(); if(str.equals("aaaaaaaa")) return 8; if(str.equals("uvtptxgtckttipjpnewpc")) return 21; HashMap hm = new HashMap(); for(int i=0,j=0;jk) { char ch2 = str.charAt(i); hm.put(ch2,hm.get(ch2)-1); if(hm.get(ch2)==0) count--; i++; } if(count == k) { ml = Math.max(ml,j-i+1); } j++; } if(ml==0) return str.length(); return ml; } }
@factsandmyths3301
@factsandmyths3301 2 месяца назад
​@@kundann_n9989no it's the same, because of 2nd condition mummy imposed on child
@AB-fr8qn
@AB-fr8qn 3 года назад
Samilar Leetcode Question :Fruit and Basket.
@rajatluthra9737
@rajatluthra9737 3 года назад
can u share the code please..?
@AB-fr8qn
@AB-fr8qn 3 года назад
@@rajatluthra9737 int totalFruit(vector& tree) { int i=0; int j=0; int n=tree.size(); int ans=INT_MIN; unordered_map m; while(j2) { m[tree[i]]--; if(m[tree[i]]==0) m.erase(tree[i]); i++; } j++; } } return ans==INT_MIN?n:ans; }
@gautamarora6556
@gautamarora6556 3 года назад
@@AB-fr8qn Hey bro could you please tell me why in last you have written return ans==INT_MIN?n:ans ? Why didn't you only return the ans?
@RomanEmpire29
@RomanEmpire29 3 года назад
@@gautamarora6556 yes we can return only ans as minimum value of ans will be 1
@balakrishnanr648
@balakrishnanr648 2 года назад
@Aditya Verma - 904. Fruit Into Baskets - LeetCode Same kind of problem, with trees and fruit.
@bluemeet8546
@bluemeet8546 3 года назад
Happiest moment of my day when I see the notification that aditya Verma has uploaded video you are legend sir
@TheAdityaVerma
@TheAdityaVerma 3 года назад
haha Thanks for watching dude ! 😅😅
@bluemeet8546
@bluemeet8546 3 года назад
singh hi kushagra yes I remember you KAISA ho
@tanmoymazumdar9354
@tanmoymazumdar9354 3 года назад
Indeed he is a legend
@utkarshverma2604
@utkarshverma2604 3 года назад
SIR GRAPH PADHA DO PLEASE PLEASEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE JULY AUG ME PLACEMENT HAI SIR JI PLEASE GRAPHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
@gautamarora6556
@gautamarora6556 3 года назад
bro graph ka sbse acha resource bta do pls muje b krna h..
@deepakffyt2844
@deepakffyt2844 Год назад
@@gautamarora6556 bhai mujhe v bata do
@gajananambekar3194
@gajananambekar3194 Год назад
Leetcode Problem: 904. Fruit Into Baskets class Solution { public: int totalFruit(vector& str) { if(str.size()==1) return 1; //edge case int n=str.size(); int i=0, j=0, res =INT_MIN; unordered_map mp; while(j < n) { mp[str[j]]++; //calculation if(mp.size() 2) //condition 3 { while(mp.size() > 2) { mp[str[i]]--; if(mp[str[i]] == 0){ mp.erase(str[i]); } i++; // if(mp.size() == 2) // res = max(res, j-i+1); } j++; } } if(mp.size()==1){ auto it = mp.begin(); return it->second; } return res; } };
@bhargav1811
@bhargav1811 Год назад
thank you
@NoDaysOff168
@NoDaysOff168 Год назад
just bit more optimised version: class Solution { public: int totalFruit(vector& fruits) { int i = 0; int j = 0; int mx = 1; unordered_map mp; while(j < fruits.size()) { mp[fruits[j]]++; if(mp.size() 2) { while(mp.size() > 2) { mp[fruits[i]]--; if(mp[fruits[i]] == 0) { mp.erase(fruits[i]); } i++; } } j++; } return mx; } };
@ornatetrout
@ornatetrout Год назад
class Solution { public: int totalFruit(vector& fruits) { int maxFruits = 0; unordered_map um; int i = 0, j = 0; while (j < fruits.size()) { um[fruits[j]]++; if (um.size() == 2) { maxFruits = max(maxFruits, j - i + 1); } else if (um.size() > 2) { while (um.size() > 2) { um[fruits[i]]--; if (um[fruits[i]] == 0) { um.erase(fruits[i]); } i++; } if (um.size() == 2) { maxFruits = max(maxFruits, j - i + 1); } } j++; } if (maxFruits == 0) { return fruits.size(); } else { return maxFruits; } } };
@manishkumar5869
@manishkumar5869 3 года назад
A slight improvement, it will not work for (a,a,a,a,a,a), so we need to update it to longest substring with atmost k unique characters.
@harshtank7075
@harshtank7075 2 года назад
100% correct
@calderanoadi5982
@calderanoadi5982 2 года назад
Ha bilkul
@likhithm3097
@likhithm3097 2 года назад
i dont think such test case should exist for this question. Clearly his mom gave a condition to pick only 2 types of toys in continuous way.And john had to maximise it that would clearly mean there were more than 2 types of toys in that row.Whats the point of his mothers conditions if all the toys are the same
@josephaj8310
@josephaj8310 2 года назад
Yes 👍
@adityajasood4891
@adityajasood4891 Год назад
can you plz help, with this code. not working correct. longest substring with at most k unique characters. plz help. import java.util.*; public class Solution { public static int kDistinctChars(int k, String str) { // Write your code here // Initialise left and right pointers int i = 0; int j = 0; int maxi = 0; while(j< str.length()) { Map map = new HashMap(); if(map.get(str.charAt(j)) == null) { map.put(str.charAt(j),1); } else { map.put(str.charAt(j), map.get(str.charAt(j))+1); } if(map.size() k) { while(map.size() > k) { map.put(str.charAt(i), map.get(str.charAt(i))-1); if(map.get(str.charAt(i)) == 0) { map.remove(str.charAt(i)); } i++; } j++; } } return maxi; } }
@sriramkrishnamurthy4473
@sriramkrishnamurthy4473 2 года назад
Fruit into baskets leetcode 👍
@venkateshjamge3471
@venkateshjamge3471 2 года назад
Want the Solutions for the playlist, Here you go: github.com/venkateshjamge/Sliding-Window-Algorithm-Aditya-Verma
@spaceman8070
@spaceman8070 3 года назад
Happiness is listening to the question at the beginning and then immediately thinking that "Ye to Longest Substring with atmost 2 unique characters hain!"
@anchitbhushan6172
@anchitbhushan6172 4 месяца назад
😂
@anchitbhushan6172
@anchitbhushan6172 4 месяца назад
😂
@deveshkumar6533
@deveshkumar6533 3 года назад
What if input only contains only one type of toys.... String:aaaaa We need to change code little bit. If ==2 condition was never hit in whole loop then length of string is answer. Edit: thanks for videos
@bhavnanagar4589
@bhavnanagar4589 3 года назад
The less than equal condition will be merged
@rafiqn2675
@rafiqn2675 3 года назад
Bhai.. Already one week but not any new video... Please upload a new video
@tanmoymazumdar9354
@tanmoymazumdar9354 3 года назад
Aditya bhaiya it has been 4 weeks since you uploaded your last video.I know you have your own work plus you are working at Flipkart and also making such good content videos is not easy .But it's our humble request please continue with such videos and covers the topics like graphs,tree, backtracking because you are the best the way you teach hardly there is an alternative to your content. We student really need your support kindly if you can make the videos in the above topics it would be of a great help🥺🙏🙏
@BikashSah
@BikashSah 2 года назад
Now, it's more than a year.
@avezqureshi14
@avezqureshi14 Год назад
@@BikashSah Now, it's more than 2 year
@anupn5945
@anupn5945 Год назад
Is bhaiya dead
@Akash-yr2if
@Akash-yr2if Год назад
@@anupn5945 Aditya Bro bewafa Hai Now its been 2 years 🥲
@ipizza9941
@ipizza9941 Год назад
@@Akash-yr2if Bro he is not obligated to make these videos. The videos he provided are free and very well organized and executed. You should be happy with the resource he has put on youtube.
@nileshgopale8528
@nileshgopale8528 3 года назад
Just after the problem statement, click the solution ( Longest Substring with K Unique Characters) Thanks @Aditya bhaiya ❤✨🤙
@nilarghyaroy1401
@nilarghyaroy1401 2 года назад
But here according to the prob statement, k is less than or equal to 2 i.e k
@PIYUSH-lz1zq
@PIYUSH-lz1zq 2 года назад
bro , que link bej do
@kumarpallav8156
@kumarpallav8156 3 года назад
Happy new year Aditya... I was so happy to see your video after such a long gap...Bhai please complete backtracking and other playlist soon :)
@TheAdityaVerma
@TheAdityaVerma 3 года назад
Happy new year Pallav :) Lets see what we have start after sliding window 😬
@TheAdityaVerma
@TheAdityaVerma 3 года назад
Btw I upload last sunday too !!
@ankita1464
@ankita1464 3 года назад
@@TheAdityaVerma Graph,Backtracking plsssssssssssssssssssssssssssssss
@rajatmittal7261
@rajatmittal7261 3 года назад
@@TheAdityaVerma graphs trees backtracking inme se bna do Kisi pr BHI or ho ske to one by one ye 3 topic cover do plz thanks in advance
@Hemanthkumar-ck1zu
@Hemanthkumar-ck1zu 3 месяца назад
What if the array has all one type of toy ,this is a edge case ig if(mp.size()
@the_hustlers_meme
@the_hustlers_meme 3 месяца назад
Yes , we should handle this test case . Because of this i got error in last test case.
@leolowe22
@leolowe22 Месяц назад
yes combine the less than and equals to case together and same applies for Fruit an basket question
@AmarjeetKumar-en1gk
@AmarjeetKumar-en1gk Год назад
jab mujhe toys nhi mile, phir mai John ko maximum kyun dilau?
@shivaniguptacv1215
@shivaniguptacv1215 3 года назад
Bro do make one playlist on greedy algorithm too.. U made DP soo easy for us please make greedy also... Plz bro
@kaustubhpimparkar8211
@kaustubhpimparkar8211 3 года назад
You are a blessing for the students preparing for the interview man! Can't thank you enough
@rkk1990
@rkk1990 3 года назад
Sir, please please make video playlist on how to identify the pattern and apply specific DS and algorithm.
@ArshadKhan-kt5ph
@ArshadKhan-kt5ph 3 года назад
Legend is back 💥
@R_023
@R_023 3 года назад
bhai backtracking par bnado plz playlist
@saikumargatla4706
@saikumargatla4706 8 месяцев назад
This problem is similar to longest substring with atmost 2 unique characters
@amiyaranjanbarik5990
@amiyaranjanbarik5990 2 года назад
Problem Description: leetcode.com/problems/fruit-into-baskets/ Problem solution: int totalFruit(vector& fruits) { int i = 0, j = 0, ans = 0; unordered_map mp; while(j < fruits.size()){ mp[fruits[j]]++; if(mp.size() 2){ mp[fruits[i]]--; if(mp[fruits[i]] == 0){ mp.erase(fruits[i]); } i++; } j++; } } return ans; }
@53_manishkumar_it54
@53_manishkumar_it54 Год назад
thank you bhai 🤝mai question hi search kr rha tha .
@anamaysrivastava1219
@anamaysrivastava1219 3 года назад
Sir, please bring in new lectures of the remaining topics .It's really a boon to all of us .
@maheshthakare2762
@maheshthakare2762 Год назад
according to this problem unique should be maximum 2 not equal to 2 So, all toys of same type will also work, the correct code would be: #include using namespace std; int main(){ string s="abaccab"; int i=0; int j=0; int k=2; map m; int mx=INT_MIN; while(j
@sourabhgautam4809
@sourabhgautam4809 Год назад
Thank you, Sir, Solve the problem in just the first 4 mins of your video explanation. Longest Subarray with 2 unique characters.
@kunal_chand
@kunal_chand 3 года назад
Bhaiya apki Doctor wali writing hai. But your explanation is really cool
@girikgarg1268
@girikgarg1268 3 года назад
So in this question, I will have to make two function calls, one with k=1 and k=2 and find out the maximum of the two, right?
@shouryade4502
@shouryade4502 9 месяцев назад
you can already do that if you have the condition for hashmap size 2 (reduce elements and slide window). Sorry for a late reply.
@thedataguyfromB
@thedataguyfromB 3 года назад
English me nahi Hindi me samjh liye hai 🤪🤪🤪😂😂 Thanks
@AbhishekSharma-me
@AbhishekSharma-me Год назад
This is somewhat similar to longest substring with k unique characters.
@adityarajiv6346
@adityarajiv6346 3 года назад
Longest subarray having atmost 2 unique elements.
@bestsaurabh
@bestsaurabh 3 года назад
or Exactly 2 unique?
@adityarajiv6346
@adityarajiv6346 3 года назад
@@bestsaurabh ahh what if there is only one kind of toy?? You also maybe correct 😀👍.
@kartikeybhardwaj3987
@kartikeybhardwaj3987 Год назад
I need 4x speed in this video
@shrinathdeva6997
@shrinathdeva6997 3 года назад
Wonderful ❤️. Brother btw can you tell is the DP series complete? Cuz I couldn't find videos for LIS, Fibonacci, kadanes and DP on grid. So can you please add those? Or if not atleast can you let us know what types of questions that could be solved with them?
@manaslahon1031
@manaslahon1031 9 месяцев назад
The code for given input- k=2 string s=abaccab //Here's the code if needed #include using namespace std; int maxToys(string s, int k){ unordered_mapm; int i=0, j=0, ct=0, mx=0; while(jk){ m[s[i]]--; ct--; if(m[s[i]]==0){ m.erase(s[i]); } i++; } } if(m.size()==k){ mx=max(mx, ct); } j++; } } return mx; } int main(){ int k; cin>>k; string s; cin>>s; cout
@arunpanjiyar76
@arunpanjiyar76 Год назад
#java import java.util.HashMap; public class Solution { public static int getLongestSubString(String s, int k) { int max = -1; HashMap map = new HashMap(); int i = 0; int j = 0; while(j < s.length()) { char ch = s.charAt(j); if(map.containsKey(ch)) { map.put(ch, map.get(ch) + 1); }else { map.put(ch, 1); } if(map.size() == k) { max = Math.max(max, j - k + 1); j++; } if(map.size() > k) { char ch1 = s.charAt(i); while(map.size() > k) { if(map.containsKey(ch1)) { map.put(ch1, map.get(ch1) - 1); if(map.get(ch1) == 0) { map.remove(ch1); } } i++; } } j++; } return max; } public static void main(String[] args) { String s = "aaaa"; int k = 2; System.out.println(Solution.getLongestSubString(s, k)); } }
@rahulgarg7966
@rahulgarg7966 3 года назад
i wish, john must watch this SLIDING WINDOW! from aditya bhai..,
@sunitasoni1554
@sunitasoni1554 3 года назад
Hey, please make more videos if you have time....yours video are like osm
@amanagarwal6897
@amanagarwal6897 3 года назад
Hi bro, Love your videos. Just a suggestion that you should make a Telegram channel if possible. A community under this channel will grow, and you'll also be able to communicate with the viewers better.
@husainchampawala4773
@husainchampawala4773 Год назад
/* John is at a toy store help him pick maximum number of toys. He can only select in a continuous manner and he can select only two types of toys. Example: Input: 1 abaccab */ /* LONGEST SUBSTRING WITH TWO UNIQUE CHARACTERS */ #include using namespace std; int pickToys(string str) { unordered_map mp; int i = 0, j = 0, maxi = 0; for (; j < str.size(); j++) { mp[str[j]]++; if (mp.size() == 2) maxi = max(maxi, j - i + 1); while (mp.size() > 2) { mp[str[i]]--; if (mp[str[i]] == 0) mp.erase(str[i]); i++; } } return maxi; } int main(int argc, char const *argv[]) { string s; cin >> s; cout
@philosphize
@philosphize 3 года назад
Sir are you planning to upload Greedy algorithm playlist??
@sribhargavram5452
@sribhargavram5452 Год назад
I didn't watched the complete video , but I think this is similar to the problem of finding longest substring with 2 unique characters. Am I right?
@shashwatrai1989
@shashwatrai1989 3 года назад
Bhaiyya ab nayi series bna bhi dijiye. Kitna rulayenge bhakto ko?😢
@bhavyagulati9293
@bhavyagulati9293 Год назад
tussi great ho paaji
@aakashsrivastava9267
@aakashsrivastava9267 4 месяца назад
Thank you so much bhaiyaaa for great explanation.❤🙏 I want to know one thing that where can i find such story based questions in order to practice.
@pritishpattnaik4674
@pritishpattnaik4674 2 года назад
#include using namespace std; int pickToys(string s){ int i=0, j=0; int maxi = INT_MIN; unordered_map mp; while (j < s.size()){ //calculation mp[s[j]]++; //cond < 2 if (mp.size() < 2){ j++; } //cond == 2 else if (mp.size() == 2){ maxi = max(maxi, j-i+1); j++; } //cond > 2 else if (mp.size() > 2){ while (mp.size() > 2){ mp[s[i]]--; if (mp[s[i]] == 0){ mp.erase(s[i]); } i++; } j++; } } return maxi; } int main(){ string s = "abaccab"; cout
@shlokhinge3179
@shlokhinge3179 3 года назад
bro, giveaway result?
@vodnalasricharan1323
@vodnalasricharan1323 3 года назад
c++ code //my version int atMostKuniqueelements(vector& A, int K) { //we can pass string in place of vector A int i = 0, res = 0; unordered_map count; //use incase of string for (int j = 0; j < A.size(); ++j) { if (!count[A[j]]++) K--; while (K < 0) { if (!--count[A[i]]) K++; i++; } res =max(res,j - i + 1); } return res; }
@gaurabdas1510
@gaurabdas1510 3 года назад
| C++ solution | #include using namespace std; //This function returns max toys that can be bought as a substring of given string having atmost k types of toys //Time complexity - O(n) and space complexity - O(k) int maxToys(string &toys, int k) { unordered_map mp; //to store count of different types of toys selected int n=toys.length(), ans=0; int i=0, j=0; //sliding window //traversing string while(j
@yashkadtan264
@yashkadtan264 3 года назад
Please start a graph theory series You have helped me so much. I need one more help and that is graph. Tried for such a long time still I am not able to solve competitive graph questions.
@chaitanyagupta6668
@chaitanyagupta6668 3 года назад
try easy ones. Remember if u are facing huge problem in some question topic, chances are u have not reached to a sufficient level. try easier ones.
@gautamarora6556
@gautamarora6556 3 года назад
@@chaitanyagupta6668 bro graph ka sbse acha resource bta do pls muje b krna h..
@gautamarora6556
@gautamarora6556 3 года назад
bro graph ka sbse acha resource bta do pls muje b krna h..
@chaitanyagupta6668
@chaitanyagupta6668 3 года назад
@@gautamarora6556 mycodeschool try karo. RU-vid channel h. And remember, graph will take time, no matter the resource. To usey dheere dheerey karna and it will feel difficult , but after u solve about 10-20 problems, asan lagega. Bahut variety nhi Hoti hai unke questions me.
@kundann_n9989
@kundann_n9989 2 месяца назад
if we were count the number of substring with atmost k characters, how would we do it?
@googleit2490
@googleit2490 Год назад
Solved before watching the video :) Sep'27, 2023 06:27 pm int totalFruit(vector& fruits) { int n=fruits.size(); unordered_mapmp; int i=0, j=0, ans=0; while(j2){ ans=max(ans, j-i); mp[fruits[i]]--; if(mp[fruits[i]]==0){ mp.erase(fruits[i]); } i++; } j++; } ans=max(ans, j-i); return ans; }
@DeepakSingh-fy3fb
@DeepakSingh-fy3fb 3 года назад
vo to sahi hai.par agar 2 unique toys present ni hai to kya print karna hai,o ya fir single toy ka maximum length ,which of course will be array length.
@vakhariyajay2224
@vakhariyajay2224 Год назад
Thank you very much. You are a genius.
@vineet4991
@vineet4991 Год назад
class Solution { public int totalFruit(int[] fruits) { int i=0; int j=0; int n= fruits.length; HashMap map = new HashMap(); int ans =0; while(j2){ while(map.size() > 2){ map.put(fruits[i], map.get(fruits[i])-1); if(map.get(fruits[i])==0){ map.remove(fruits[i]);} i++; }j++; } }return ans; } }
@roshangupta8161
@roshangupta8161 8 месяцев назад
Mummy ji made this question interesting
@parthh1112
@parthh1112 7 месяцев назад
class Solution { public: int totalFruit(vector &v) { int ans = 0, i = 0, j = 0, sum = 0; unordered_map u; while (i < v.size()) { u[v[i]]++; if (u.size() > 2) { u[v[j]]--; if (u[v[j]] == 0) u.erase(v[j]); j++; } int s = 0; for (auto k : u) s += k.second; // o(1) => loop is running for only 2 elements ans = max(ans, s); i++; } return ans; } };
@AyushGupta-ux4gq
@AyushGupta-ux4gq 8 месяцев назад
C++ class Solution { public: int totalFruit(vector& fruits) { int n=fruits.size(); int i=0,j=0; int maxi=INT_MIN; map mpp; while(j2) { while(mpp.size()!=2) { --mpp[fruits[i]]; if(mpp[fruits[i]]==0) { mpp.erase(fruits[i]); } ++i; } } if(mpp.size()
@zenetictech2331
@zenetictech2331 Год назад
// leetcode fruits into basket class Solution { public: int totalFruit(vector& fruits) { int i=0,j=0; unordered_mapm; int maxi = 0; while(j 2){ m[fruits[i]]--; if(m[fruits[i]] == 0) m.erase(fruits[i]); i++; } } j++; } return maxi; } };
@Bzgwk
@Bzgwk 11 месяцев назад
FRUIT IN BASKET LEETCODE JAVA class Solution { public int totalFruit(int[] fruits) { int i=0; int j=0; int n= fruits.length; int ans=0; Map map= new HashMap(); if(fruits.length==1) return 1; while(j2){ int val= map.get(fruits[i]); val=val-1; if(val==0){ map.remove(fruits[i]); } else{ map.put(fruits[i],val); } i++; } if(map.size()==2){ ans= Math.max(ans,j-i+1); j++; } } } return ans; } }
@AyushGupta-ux4gq
@AyushGupta-ux4gq 8 месяцев назад
class Solution { public: int totalFruit(vector& fruits) { int n=fruits.size(); int i=0,j=0; int maxi=INT_MIN; map mpp; while(j2) { while(mpp.size()!=2) { --mpp[fruits[i]]; if(mpp[fruits[i]]==0) { mpp.erase(fruits[i]); } ++i; } } if(mpp.size()
@AyushGupta-ux4gq
@AyushGupta-ux4gq 8 месяцев назад
class Solution { public: int totalFruit(vector& fruits) { int n=fruits.size(); int i=0,j=0; int maxi=INT_MIN; map mpp; while(j2) { while(mpp.size()!=2) { --mpp[fruits[i]]; if(mpp[fruits[i]]==0) { mpp.erase(fruits[i]); } ++i; } } if(mpp.size()
@raghavagrawal9240
@raghavagrawal9240 2 года назад
Chithhi na koi Sandesh .. Jaane vo kaun desh jaha tum chalegaye 😰
@ROHIT-gv7xk
@ROHIT-gv7xk 3 года назад
bhaiya next kisi topic pr bnado tree, graph, backtracking plz placements aane wali hai
@neerajkumar-ik3vh
@neerajkumar-ik3vh 3 года назад
always waiting for your videos. Thanks
@malkeetsapien4852
@malkeetsapien4852 2 года назад
Beautifully Taught !
@tokirmanva2247
@tokirmanva2247 3 года назад
length of longest subarray with two different toys.
@srinivasbeta8202
@srinivasbeta8202 Год назад
solved the question without seeing the video coz i watched the previous videos ...... this guy is legend
@priyanshkumar17
@priyanshkumar17 10 месяцев назад
Yes, indeed
@omk6258
@omk6258 3 года назад
Longest increasing subsequence
@ritwik121
@ritwik121 3 года назад
subsequence kaise hoga hoga . substring ho sakta he
@ritwik121
@ritwik121 3 года назад
subsequence kaise hoga hoga . substring ho sakta he
@ankitaakodiya6074
@ankitaakodiya6074 3 года назад
Happy New year Aditya boss
@herculean6748
@herculean6748 Год назад
kaash bhaiya college me hote toh abhi bhi videos bana paate!
@pranavsharma7479
@pranavsharma7479 2 года назад
fruits into basket is same as this
@sarangavadiya7087
@sarangavadiya7087 Год назад
This question is same as Longest_K_unique_characters_substring but just K=2
@21sivansrawat
@21sivansrawat Год назад
but with one more condition to update max when map.size() < 2
@sumantkumarpandey3818
@sumantkumarpandey3818 Год назад
Question waighrah to thik ye shraddha ka kya karna hai
@vipulgupta3915
@vipulgupta3915 3 года назад
Working Code: public static void findMaxToysCount(String str) { int i=0,j=0; HashMap map = new HashMap(); int maxCount = 0; while(j
@Bzgwk
@Bzgwk Год назад
if anyone is looking for the JAVA solution public class PickToys { public static void main(String[] args) { // Variable window // conditions -> toy ko line se uthana hai aur maximum two type ka toy hi hona chahiye // no of unique toy should be two and we should give john max toys String str="abaccacb"; // each character represents a type of toy // a-> car // b->helicopter // c->elephant int maxToys= Integer.MIN_VALUE; int k=2; HashMap map= new HashMap(); int i=0,j=0; while(jk) { int val= map.get(str.charAt(i)); val--; if(val==0) { map.remove(str.charAt(i)); i++; } else { map.put(str.charAt(i), val); } } j++; } } System.out.println(maxToys); } }
@NotNotNithin
@NotNotNithin 2 года назад
Working Java Solution: public static int findMaxToysToPick(String s) { Map frequencyMap = new HashMap(); int windowStart = 0, windowEnd = 0, maxLength = Integer.MIN_VALUE; while (windowEnd < s.length()) { char rightChar = s.charAt(windowEnd); frequencyMap.put(rightChar, frequencyMap.getOrDefault(rightChar, 0) + 1); if (frequencyMap.size() < 2) { windowEnd++; } else if (frequencyMap.size() == 2) { maxLength = Math.max(maxLength, windowEnd - windowStart + 1); windowEnd++; } else { while (frequencyMap.size() > 2) { char leftChar = s.charAt(windowStart); frequencyMap.put(leftChar, frequencyMap.get(leftChar) - 1); if (frequencyMap.get(leftChar) == 0) { frequencyMap.remove(leftChar); } windowStart++; } } } return maxLength; }
@AnkitGuptaYoutube
@AnkitGuptaYoutube 9 месяцев назад
Same question as Longest Substring With K Unique Characters | Variable Size Sliding Window with K=2
@Akash-yr2if
@Akash-yr2if Год назад
Not everyone is blessing with an ADITYA VERMA during an OA
@aishwaryadwani9365
@aishwaryadwani9365 3 года назад
Longest substring with atmost k unique characters - just plotted a story of toy on that 😪
@aditya.bhadauriya
@aditya.bhadauriya 3 года назад
Eagerly waiting for next video.
@BhavyaJain-qz8jg
@BhavyaJain-qz8jg 2 месяца назад
coded it myself, blessed by prev videos.. Kudos to @TheAdityaVerma :))
@beinghappy9223
@beinghappy9223 2 года назад
Fruit into Baskets
@rajnisharma4429
@rajnisharma4429 Год назад
Fruites in basket
@2amCoder
@2amCoder 7 месяцев назад
1:03 ? shradda sabke saath hai?
@bhaskararya9112
@bhaskararya9112 3 года назад
There is a problem or maybe a possible problem in last 3 ques...longest k unique characters(Q1), longest unique characters without repetition(Q2), and this picking toys(Q3)....in all the 3 ques when we are exceeding the condition i.e. when(m.size()>k) {for Q1}, when(freq of a char is>1) {for Q2}, similar for q3, we are just doing j++, so while doing this we are skipping the possible case of i to j before doing j++....for ex....take aabacbe an ex of Q1 i=0,j=0 {a} ; m.size()k ; so then, i=2 and j was 5 ; after this we are just doing j++ and we exit the loop ; rather, after i becomes 2 and j was 5, we should first do mx=max(mx,j-i+1) -> mx=max(4,6)=6 {so here it might have been possible in some test case that j-i+1 could have been > 6 but acc to explanation in video we are skipping it! } ; then j++; Please correct me if I am missing something here...my code was accepted before i thought all this, then I implement this whole thing I just explained and my code still got a/c, so I am wondering why my code got a/c the first time without implementing all this ; what i believe happened is there might be no test case where this happens, or this thing never happens.
@ManishKumar-ux5un
@ManishKumar-ux5un 3 года назад
@Aditya sir, please improve your way of notation, you even denoted continuous as 'contd'......
@rafiqn2675
@rafiqn2675 3 года назад
Bro why are you not uploading videos...bhaiu are the hope please upload yaar...Please consider backtracking
@jokerr3751
@jokerr3751 3 года назад
To find whether a subarray is present or not whose sum is equal to zero.. Sir, please include this problem in the playlist..
@shivalikagupta3433
@shivalikagupta3433 2 года назад
Longest substring with k unique characters hi hai ye , as far as I have listened the problem statement
@adityan5302
@adityan5302 2 года назад
This is same as max subarray with k unique characters edit : saying this before seeing the explanation part
@roshangupta8161
@roshangupta8161 8 месяцев назад
In a nutshell we need to find the longest substring with 2 different characters. !!
@rollercoaster9719
@rollercoaster9719 Год назад
maximum substring length with two unique characters
@rishabhgupta9846
@rishabhgupta9846 Год назад
thanks bhai, after explanation solution click kar gya mujhe
@mansidixit1851
@mansidixit1851 Год назад
Hey Aditya would be very helpful if you can add videos of bit manipulation
@pritishpattnaik4674
@pritishpattnaik4674 2 года назад
I think I got the logic , before watching the complete video