Тёмный

L5. Fruit Into Baskets | 2 Pointers and Sliding Window Playlist 

take U forward
Подписаться 681 тыс.
Просмотров 67 тыс.
50% 1

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...

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

 

29 сен 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 100   
@Sandeep-tn8mz
@Sandeep-tn8mz 6 месяцев назад
This was asked to me in AMAZON
@himanshu_047
@himanshu_047 5 месяцев назад
You got selected??
@Sandeep-tn8mz
@Sandeep-tn8mz 5 месяцев назад
@@himanshu_047 NO bro .. Im not from CS background so, my CS fundamentasl are not strong.. especially OSI, System design.. I was able to solve the current problem but messed up in system design problem.
@oppie335
@oppie335 5 месяцев назад
@@himanshu_047 he's here so , you know..
@Shivam-ux8mn
@Shivam-ux8mn 5 месяцев назад
Bhaiya mai abhi class 12 pass kiya haii aur abhi btech cse krne ka decide kiya hu and in jee mains meri 73 percentile bani aur abhi mai drop lu ya na lu ke doubt mw hu , ek point ye kehta hai ki college se zada skills matter krti hai agar tumhare pass skills hai to tumhari placement off campus bhi ho jayegi but ek taraf man me ye bhi hai ki kya ek baar try krna chhaiye , is taking a drop is worth it?
@Sandeep-tn8mz
@Sandeep-tn8mz 5 месяцев назад
Hey bro , At the end of the day your skills matter more than any college bro. Yes , the top tier colleges will give you that early career boost but its you who has to push yourself. Irrespective of college , if you keep yourself pushing you can do it from any college. yes you will have more difficulties , but they will definitely teach you a good lesson bro... ALL THE BEST (And if you are confident that you can do better by taking drop, go for it. You are still young, don't think much about outside noise. At the end of the day Its you who has to feed your family. )
@AkankshaGupta-zn7he
@AkankshaGupta-zn7he 6 месяцев назад
Just by watching & understanding the previous 4 videos, I was able to solve it on my own. First with brute then the sliding window technique. That's the magic of Striver😃❤
@jimit2795
@jimit2795 3 месяца назад
how did you come to know that you have to use map or set in it.
@darkistheknight
@darkistheknight 3 месяца назад
@@jimit2795 Look I am a noob myslef, but consider this, wherever we have to maintain something in terms of numbers like k distinct, at most k, you should know it is gonna be map. Here also we have two baskets i.e. we can store maximum of two types and we have to maximize the numbers of those 2 types of fruits. So at most K type questions. Hence map/set. That's all there's to it. Don't worry keep grinding. You'll get it.
@TheAditya-zt9pb
@TheAditya-zt9pb 2 месяца назад
@@darkistheknight exactly your intuition is similar to mine
@ArjunAR06
@ArjunAR06 3 месяца назад
I'm visiting this problem after a week (I solved this by watching striver's solution). I was not able to solve a single problem on my own thinking. I saw few comments stating that they were able to solve this by themselves and I was like "Am I doing something wrong. Why am I not able to solve on my own?" To my surprise, I completed the entire sliding window playlist and when I revisited after like a week, I'm able to solve every single sliding window problem in like 20 minutes. You deserve a huge salute, Striver!
@CleancodeCentral
@CleancodeCentral 2 месяца назад
I used to have the Same problem manhh. Thats Normal. Complete the whole series or half of the problems. then take a problem and try to solve the problem on paper and pen. For some it takes only 50 leetcode problems But for some it takes More than 200 . " Consistency is the Key". keep improving...
@aryanjain9432
@aryanjain9432 Месяц назад
@@CleancodeCentral Tiru
@AK-nj1je
@AK-nj1je 3 месяца назад
Actually the space complexity of the most optimal approach will be almost O(n) Coz just imagine all the elements are different then, the code will not care what's the size of the map, it will just keep adding elements in the map, howeven the maxlen will not be affected. Amazing lecture!!!!
@shubhtandon8315
@shubhtandon8315 2 месяца назад
Right!!! but suppose the array has half no. of elements as of type 1 and the rest of the array has different types(2,3,4,.....) then while trimming down the left side we will be adding different elements to the map. So instead of O(n), it can said to be near about O(maxlen ) or O(n/2) as at max we will be storing half of the elements. What do you say?
@Aparichi78gft
@Aparichi78gft Месяц назад
No bro map will not store the let say 1--- element rather it will store the entry of it so it will o(1) not o(n/2) so at max map will be o(3).
@sonakshibajpai6445
@sonakshibajpai6445 3 месяца назад
Thank you striver !!
@akworld2739
@akworld2739 4 месяца назад
but i am happy with 0(2N) time complexity😂😂😂😂
@namannema3349
@namannema3349 5 месяцев назад
thanks striver
@lucifee9407
@lucifee9407 4 месяца назад
Striver if you are the interviewer ,you ask to optimize it to 2N to N ??
@Harsh-jc2bz
@Harsh-jc2bz 4 месяца назад
mostly dont ask...9 out of 10
@rlm3227
@rlm3227 3 месяца назад
understood
@angeldeveloper
@angeldeveloper 6 месяцев назад
🙌🏻
@yashwanthkumar2513
@yashwanthkumar2513 4 месяца назад
+1
@NoFormalities-y1k
@NoFormalities-y1k 5 месяцев назад
This was asked to me in Adobe.
@himage6540
@himage6540 14 дней назад
did you clear it?
@raghavmanish24
@raghavmanish24 3 месяца назад
understood
@techmatein
@techmatein 2 месяца назад
i think i solved it in better way : class Solution { public: int totalFruit(vector& fruits) { pairp1={-1,-1}; pairp2={-1,-1}; int l=0; int ans=-1; for(int i=0;i
@selviraghavi-jp4ep
@selviraghavi-jp4ep 4 месяца назад
This question was asked in infosys specialist programmer role and bro made a video thankfully :)
@shankarrakh7744
@shankarrakh7744 2 месяца назад
(best solution without using map) int totalFruits(int N, vector &fruits) { int f = -1; // first fruit type int s = -1; // second fruit type int l = 0; // left pointer int r = 0; // right pointer int ans = 0; while (r < N) { if (f == -1 || fruits[r] == fruits[f]) { f = r; } else if (s == -1 || fruits[r] == fruits[s]) { s = r; } else { // When a new fruit type is encountered if (f < s) { l = f + 1; f = r; } else { l = s + 1; s = r; } } ans = max(ans, r - l + 1); r++; } return ans; }
@RadheShyam33455
@RadheShyam33455 Месяц назад
tried using set instead of hashset and directly removing(arr[i]) from set and setting i=j-1; whenever setSize >2 but it failed certain test cases
@anshkothariAK
@anshkothariAK Месяц назад
Your code will not work for this testcase: {1, 2, 1, 2, 3, 2, 2, 1}
@RadheShyam33455
@RadheShyam33455 24 дня назад
Then I realised removing in set may remove the unintended elements also .
@rohanbera6227
@rohanbera6227 Месяц назад
int totalFruits(vector arr) { int n = arr.size(); if (n == 1) return 1; int i = 0, j = 0, maxi = 0; int elem1 = arr[0], elem2 = -1; int ind1 = 0, ind2 = 0; int lastConsec = 0; while (j < n) { if (elem2 != -1 && elem1 != arr[j] && elem2 != arr[j]) { i = lastConsec; elem1 = arr[i]; elem2 = arr[j]; } elem2 = (elem1 != arr[j] && elem2 == -1) ? arr[j] : elem2; if (arr[j] != arr[lastConsec]) lastConsec = j; maxi = max(maxi, j - i + 1); j++; } return maxi; }
@phucphanphamtrong4686
@phucphanphamtrong4686 18 дней назад
This question is same as the question longest substring with at most K distinct character, and leet code charge that question for premium LOL =))
@soumeshnayak4546
@soumeshnayak4546 3 месяца назад
I was unable to solve a single question in Sliding Window and two pointers after watching the above concepts I can think of and able to solve this question. This is the solution that I have solved on my own. def totalFruit(self, fruits: List[int]) -> int: maxlen=0 map={} i=0 for j in range(len(fruits)): map[fruits[j]]=map.get(fruits[j],0)+1 while len(map)>2: map[fruits[i]]-=1 if map[fruits[i]]==0: del map[fruits[i]] i+=1 if len(map)
@rajharsh3739
@rajharsh3739 6 месяцев назад
I just solved before seeing this video by just copy paste the last code from L4 and did some tweaks. 🙌🙌
@Ethical-Boy-No-1
@Ethical-Boy-No-1 6 месяцев назад
28:58 I have a question bhaiya .. Let's take this example {1,2,1,2,1,2,1,2,3,4,5,6,7,8,9,10,11} .. if you see in case of optimal solution then SC=O(n/2) for map ... so how it is O(1) . And if you make an example of 10^5 length array the same as the upper example then also SC=O(10^5/2)..
@tanujaSangwan
@tanujaSangwan 22 дня назад
I solved this question in 5-8 mins without even starting the window. All the consecutive type questions are so intuitive using sliding window. Used pq and map to solve it. int totalFruits(vector &arr) { unordered_map mp; int maxlength=0; int left=0; for(int right=0; right
@darkwarrior6767
@darkwarrior6767 День назад
This was asked to me in Microsoft
@NirbhayShah-oc2ww
@NirbhayShah-oc2ww 22 дня назад
In last solution, how is the space complexity O(1). Suppose there is a array with 200 elements, first 100 are 1 and 2 alternating. Rest 100 are 3, 4, 5, ............ While iterating my map will have only 2 elements till the 100th element of array, then other elements will be added and when at 198th element, I will have 100 elements in my map. That is definitely not constant
@nishantshrivastava8937
@nishantshrivastava8937 3 месяца назад
Theoretically, we have optimised the solution to O(n) but the solution of O(2N) is faster on runtime while programming.
@kishoregowda8210
@kishoregowda8210 27 дней назад
Yeahhh even i was wondering why
@sahulraj9536
@sahulraj9536 Месяц назад
for optimal approach space complexity cant be O(1) right, because map can have more than 2 elements as well for this example {1,1,1,1,1,3,3,2,4,5,6,7,8,9} for the above example map can have almost n/2 elements correct me if im wrong
@Aparichi78gft
@Aparichi78gft Месяц назад
No bro map will have at max 3 elements in any case
@verma_jay
@verma_jay Месяц назад
You are correct
@5Q8NAZEER
@5Q8NAZEER Месяц назад
we should erase mp.erase(arr[l]) not mp.erase(mp[arr[l]]) ...... a small correction from my side
@deepakvajpayee2396
@deepakvajpayee2396 5 месяцев назад
This solution can also be optimised by in place of storing count of the character in map if we store last index of the character
@stith_pragya
@stith_pragya 5 месяцев назад
UNDERSTOOD...Thank You So Much for this wonderful video.......🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@raghavmanish24
@raghavmanish24 3 месяца назад
hey striver you make things so simple , some time i got jealous that some others also can know this method from this videos ........thanku again striver
@2amCoder
@2amCoder 6 месяцев назад
althugh i have alredy did all these patterns questions stii here for your support ! thanks a lot for that dp and graph series and others too.
@fizzgaming7910
@fizzgaming7910 3 месяца назад
Pointing out a small error, instead of erasing mpp[arr[l]] in the code, erase arr[l] and the code works flawlessly
@Dhjsjebvebehw
@Dhjsjebvebehw 25 дней назад
omg...i was recently asked this in atlassian!!! dayummm
@akashwasson4220
@akashwasson4220 4 месяца назад
Striver bro you are a pro....Salute!
@noone-fl1qb
@noone-fl1qb 5 месяцев назад
Can anyone tell me why this is 'nt working int findMaxFruits(vector &arr, int n) { int left = 0, right = 0, maxLength = 0; unordered_map map; while (right < arr.size()) { map[arr[right]]++; if (map.size() > n) { map[arr[left]]--; if (map[arr[left]] == 0) { map.erase(arr[left]); } left++; } if(map.size()
@shubhiiii220
@shubhiiii220 4 месяца назад
Add "right++" after closing the bracket of "if(map.size()
@ayushisingh6638
@ayushisingh6638 4 месяца назад
If this is geeksforgeesks question the varibale n is the size of array. if questio says 2 basket then you need to use 2 instead of n the following will produce correct result: int totalFruits(int n, vector &arr) { int left = 0, right = 0, maxLength = 0; unordered_map map; while (right < n) { map[arr[right]]++; if (map.size() > 2) { map[arr[left]]--; if (map[arr[left]] == 0) { map.erase(arr[left]); } left++; } maxLength = max(maxLength, right - left + 1); right++; } return maxLength; }
@jagansubramanian5234
@jagansubramanian5234 6 месяцев назад
You made my DSA journey easy
@garvsharma1210
@garvsharma1210 18 дней назад
22:30
@Captionamerica
@Captionamerica 2 месяца назад
Bhai saaahab tussi great ho ❤❤❤❤. A big hearts to you bade bhaiyaaa
@hashcodez757
@hashcodez757 Месяц назад
"UNDERSTOOD BHAIYA!!"
@watch2-grow
@watch2-grow Месяц назад
Understood thanks bhaiya
@StudyYuv
@StudyYuv 2 месяца назад
lovee you brooo thanksss a lott!!!
@takagaminggxd
@takagaminggxd 2 месяца назад
beautiful explanation ❤
@DhineshKaviraaj-l9z
@DhineshKaviraaj-l9z 6 месяцев назад
Please add C++ code also
@parvahuja7618
@parvahuja7618 6 месяцев назад
then what will you do
@sangharshpipare666
@sangharshpipare666 5 месяцев назад
@@parvahuja7618 I will dance
@ramdangi5335
@ramdangi5335 5 месяцев назад
@@parvahuja7618 nagin dance 😂😂
@lakshsinghania
@lakshsinghania 4 месяца назад
do it yourself bro
@oyeesharme
@oyeesharme Месяц назад
understood bhaiya
@txbankush4601
@txbankush4601 3 месяца назад
can anyone provide me the full code because my some testcases are not being passed according to this pseudocode
@AJ-ve1yb
@AJ-ve1yb 5 месяцев назад
should i follow sequence of this playlist according to a2z sheet or to watch first which has been uploaded first @striver
@alessandrocamilleri1239
@alessandrocamilleri1239 5 месяцев назад
Great explanation as usual. However the map is actually not necessary. int findMaxFruits(vector &arr, int n) { int fruit1 = arr[0], fruit2 = -1; int nextLeftPos = -1; int l = 0, r = 1; int count = 1; while (r < n) { if (arr[r] == fruit1 || arr[r] == fruit2) { if(arr[r] != arr[r-1]) nextLeftPos = r; count = max(count, r - l + 1); r++; } else if (fruit2 == -1) { fruit2 = arr[r]; nextLeftPos = r; count = max(count, r - l + 1); r++; } else { l = nextLeftPos; fruit1 = arr[l]; fruit2 = arr[r]; } } return count; }
@shreerangaraju1013
@shreerangaraju1013 4 месяца назад
Can anyone tell me what iPad software is striver using?
@adityapandey23
@adityapandey23 2 месяца назад
Understood
@harshit779
@harshit779 5 месяцев назад
bahaiya please upload article on the website for this
@Abcd-jt1qs
@Abcd-jt1qs 3 месяца назад
Understood sir! Thank you :)
@hardiksingh8344
@hardiksingh8344 5 месяцев назад
thanks sir can you update in take you forward site as well
@shivisingh9975
@shivisingh9975 5 месяцев назад
Understood sir!
@AtulSingh-dp8wo
@AtulSingh-dp8wo 6 месяцев назад
most awaited ❤thanks a lot!
@augustinradjou3909
@augustinradjou3909 6 месяцев назад
Understood bhaiya ❤
@AkashKumarTiwary-u4b
@AkashKumarTiwary-u4b 4 месяца назад
god
@shailesh_rajpurohit
@shailesh_rajpurohit 6 месяцев назад
understood
@saketjaiswal9381
@saketjaiswal9381 5 месяцев назад
done
@ramakrishnakcr4417
@ramakrishnakcr4417 6 месяцев назад
understood
@subee128
@subee128 6 месяцев назад
thanks
@angeldeveloper
@angeldeveloper 6 месяцев назад
❤🙌🏻👍
@Beeplov2337568
@Beeplov2337568 6 месяцев назад
😍
@simarpreetsingh7235
@simarpreetsingh7235 Месяц назад
A better method could be storing {value, last picked position} , since only two values would be there in the map pick the minimum of last picked position of the two, O(N) solution
@techmatein
@techmatein 2 месяца назад
after solving the previous question on my own and reading this one, i thought i could not be able to solve this one on my own but gave it a try and solved it on my own and that also in just o(n). quite happy today.
Далее
Почему?
00:22
Просмотров 150 тыс.
КАК БОМЖУ ЗАРАБОТАТЬ НА ТАЧКУ
1:36:32
Triton Conference 2024: Morning Session
1:37:43
LeetCode was HARD until I Learned these 15 Patterns
13:00
5 Math Skills Every Programmer Needs
9:08
Просмотров 1 млн