G-10. Rotten Oranges | C++ | Java

  Рет қаралды 277,620

take U forward

take U forward

Жыл бұрын

GfG-Problem Link: bit.ly/3oekoir
C++/Java/Codes and Notes Link: takeuforward.org/data-structu...
DP Series: • Striver's Dynamic Prog...
SDE Sheet: takeuforward.org/interviews/s...
Check out our Website for curated resources:
Our Second Channel: / @striver_79
In case you are thinking to buy courses, please check below:
Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
---------------------------------------------------------------------------------------------------------------------------

Пікірлер: 362
@takeUforward
@takeUforward Жыл бұрын
Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞 Do follow me on Instagram: striver_79
@animecontent2056
@animecontent2056 Жыл бұрын
Don't have an account in Instagram and it distracts me much more than anyone does 😎😎😎
@aditidey5227
@aditidey5227 Жыл бұрын
Understood
@herculean6748
@herculean6748 Жыл бұрын
I am yet to start Tree and Graph, should start graph first? or do I need to do tree first?
@getgoin2217
@getgoin2217 Жыл бұрын
really appreciate you putting quality content , but did not understand from explanation so clarifying here my understanding the TC is O(n*m) because of adding starting points in the queue + O(n*m*4) is because of BSF also worst case is not all oranges being fresh but 1 orange being rotten with rest all being fresh
@Codebond7
@Codebond7 Жыл бұрын
where is java code I am unable to find
@sidduroy9150
@sidduroy9150 Жыл бұрын
I can complete the whole playlist in one day , coz it's just like a Webseries 😂😊, Offcourse it's only bcoz of the Lead character: striver
@manishprajapati8544
@manishprajapati8544 Жыл бұрын
😂😂😂
@vishakhas1867
@vishakhas1867 Жыл бұрын
Agreed bro
@monumishra9638
@monumishra9638 Жыл бұрын
Agree
@sidduroy9150
@sidduroy9150 Жыл бұрын
@@monumishra9638 hey guys , how far your preparation came
@sidduroy9150
@sidduroy9150 Жыл бұрын
@@vishakhas1867 how far
@av21015
@av21015 Жыл бұрын
I really appreciate your dedication. This video is already recorded in stacks and queues playlist but you have recorded it again here. And this video is better than previous one.
@takeUforward
@takeUforward Жыл бұрын
Yes, true, I agree
@patrickishere
@patrickishere Жыл бұрын
​@@takeUforward which tool do you use to explain? it is so good !
@SatyaMantha
@SatyaMantha Жыл бұрын
Striver, I like the way you make toughest problems look so easy with your explanation. Kudos to you!
@vaibhavagarwal2421
@vaibhavagarwal2421 Жыл бұрын
instead of using pair we can use vis grid to store the time as well
@suheabkhan2546
@suheabkhan2546 Жыл бұрын
understood bahut chota compliment hai.... Dil khush hojata hai aap say padke bhai
@RohitKumar-dz8dh
@RohitKumar-dz8dh 11 ай бұрын
For sure no one need to worry about code in any language after getting approach how to tackle problem 😊. Thanks again .
@nipu8406
@nipu8406 Жыл бұрын
Thanku sir for interview centric problem solving on graph because most of the UTube channels have algorithm of graph with no problem solving based on the concepts .
@johnj171
@johnj171 Ай бұрын
really great but what is exceptional is the connection you create between every problem and the volume of problems solved it builds up a strong foundation for every one slowly and strongly highly recommended for every one!!!!!!!!
@rishabhsingh3873
@rishabhsingh3873 Жыл бұрын
for delRow and delCol we can use array directions = {-1, 0, 1, 0, -1} and inside for loop for neighbor row and column we can use int nRow = r + directions[i]; int nCol = c + directions[i+1];
@shiblijamal8544
@shiblijamal8544 6 ай бұрын
Anything is fine make sure the coordinates traverse all those direction that's it... Btw good thinking
@LowkeyCoder
@LowkeyCoder 6 ай бұрын
cool stuff!
@iflifewasmovie4766
@iflifewasmovie4766 Жыл бұрын
I was waiting to not start too soon and still see I am here in 10th lecture and it feels like a piece of cake this time....ALL THANKS TO YOU!! Waiting for all the videos..(DESPERATELY) ❤❤
@human75788
@human75788 Жыл бұрын
You are so good that I solved it in 1st attempt just thinking the right way which you taught us to do all these times. Thanks. I learned the whole graph from you.. for this question I watched only 1 minute of your video. haha!
@vaishnavimore4860
@vaishnavimore4860 Жыл бұрын
Thank you for making such great playlist!! Completely understood🚀
@DPCODE72
@DPCODE72 Жыл бұрын
In 20:35, Striver was trying to say about The industry's requirements over the data. While teaching he wants to teach about everything that is required for a Company.
@takeUforward
@takeUforward Жыл бұрын
Yes, in interviews they do care about these small things.
@DPCODE72
@DPCODE72 Жыл бұрын
@@takeUforward While(1){ cout
@Sp_Global-oc4tf
@Sp_Global-oc4tf Ай бұрын
@@DPCODE72 ; kon dega?
@ujjawaltyagi8540
@ujjawaltyagi8540 10 ай бұрын
understood !! Absolutely nailed the question (made it an easy cake walk )
@adebisisheriff159
@adebisisheriff159 6 ай бұрын
Thanks so oooooooooooooooooooooooooooooooooooooooo much Striver. We love you!!!
@cinime
@cinime Жыл бұрын
Understood! So awesome explanation as always. I guess this might one of the way to fill an area of one color with a different color in 2D field.... Anyway, thank you very much for your explanation!!
@KratosProton
@KratosProton Жыл бұрын
Striver sir, can you please also cover LC 909. Snakes and Ladders in your playlist too, nobody on yt has explained this problem yet. Just a humble request
@NaveenKumar-os8dv
@NaveenKumar-os8dv Жыл бұрын
it seems easy when we see the video, but it is difficult when it comes to actually code, but I think I am getting better. It's really just the game of converting your "thinking into coding". If we can do this, we can solve any problem.
@thatindianboy2817
@thatindianboy2817 Жыл бұрын
How do you ensure the resultant answer is minimum?
@Harshanandita
@Harshanandita Жыл бұрын
Based on the question, in 1 sec any rotten orange could rotten only its top-bottom or left-right neighbours So, it's simple step by step calculation, done using BFS
@ashwanisharma8903
@ashwanisharma8903 Жыл бұрын
@@thatindianboy2817 Bcz BFS is level order traversal. At each level we are keeping time as same. Same bcz when we dequeue one coordinate for all its four directions we will do +1 to value that was dequeued. So each level will have same time.
@rahulsidhu5945
@rahulsidhu5945 Жыл бұрын
Striver you are awesom, and that's why you are my inspiration. Thank you for everything🙂🙂
@oqant0424
@oqant0424 Жыл бұрын
Understood! So awesome explanation as always
@kritikarawat2180
@kritikarawat2180 Жыл бұрын
Very easy explaination.Loved your explaination brother.
@tasneemayham974
@tasneemayham974 5 ай бұрын
THANK YOUUU STRIVERRR!!!! Python Code for those who need it: class Solution(object): def orangesRotting(self, grid): ROWS, COLS = len(grid), len(grid[0]) vis = [[0] * COLS for i in range(ROWS)] q = deque() cntFresh = 0 for i in range(ROWS): for j in range(COLS): if grid[i][j] == 2: q.append((i,j,0)) vis[i][j] = 2 if grid[i][j] == 1: cntFresh += 1 maxTime,cnt = 0,0 delRow = [-1,0,+1,0] delCol = [0,+1,0,-1] while q: r,c,t = q.popleft() maxTime = max(maxTime,t) for i in range(4): nRow = r + delRow[i] nCol = c + delCol[i] if nRow >= 0 and nRow < ROWS and nCol >= 0 and nCol < COLS and vis[nRow][nCol] == 0 and grid[nRow][nCol] == 1: vis[nRow][nCol] = 2 q.append((nRow,nCol,t+1)) cnt += 1 if cnt != cntFresh: return -1 return maxTime
@shashank820
@shashank820 Жыл бұрын
Understood 🤓 10 lectures done. Waiting for next videos bhaiya 🙃
@AryanGairola-th3qc
@AryanGairola-th3qc 3 ай бұрын
understood done a very lengthy code because of you thankyou man...
@FooBar-lb5wf
@FooBar-lb5wf 20 күн бұрын
Thanks, Striver. I can think of a couple of optimizations. First, we can avoid the last nested loop to check if all the fresh oranges are now rotten by just keeping two counters; freshcnt and rotcnt, with freshcnt representing the initial count of fresh oranges (can be counted in the initial nested loops) and rotcnt representing those fresh oranges which were rotten after coming in contact with rotten oranges (can be updated in the BFS logic when we update the visited array). If the two counts do not match, we can return -1. The second optimization is around the udpate of time t. We should be able to safely update t with the new value since BFS order would ensure that all the nodes with time 'ts' are processed before the nodes with time 'ts+1'. So the value of t should be monotonic in nature and so checking max(tm, t) may be redundant.
@b_31mahammadzubair81
@b_31mahammadzubair81 Жыл бұрын
Understood completely 🔥🔥 and waiting for the next video
@sejalrai30
@sejalrai30 Жыл бұрын
pls jaldi jaldi laaiye graphs ke or lecture....you r the best explainer
@priyanshkumariitd
@priyanshkumariitd 2 ай бұрын
understood !! Wonderfully explained... Thanks Striver Bhaiya :)
@Saurav_Kumar514
@Saurav_Kumar514 Жыл бұрын
Amazing explanation man! 🔥👌
@gabru6809
@gabru6809 Жыл бұрын
When would the whole series be uploaded? 😅
@ManishKumar-zk5ky
@ManishKumar-zk5ky Жыл бұрын
sir your explanation is very good Thank you🤩
@shuvbhowmickbestin
@shuvbhowmickbestin 10 ай бұрын
One thing, instead of creating a visited 2d matrix we can also take a set which stores pairs of coordinates visited already, that makes it easier to maintain.
@subhranilsamanta2862
@subhranilsamanta2862 Жыл бұрын
Understood the whole Series.👏🏽
@soumikpaul6503
@soumikpaul6503 6 ай бұрын
The best video to understand BFS
@divyanshd5557
@divyanshd5557 Ай бұрын
it could have been done by the vector as well..just instead of vector vis it should be vector vis(n,vector(m,0))
@nathonluke4058
@nathonluke4058 Жыл бұрын
Understood. Also could i used queueq; data strucutre. Will it impact on Runtime Complexity or Space?
@pratikdas1780
@pratikdas1780 Жыл бұрын
UNDERSTOOD SIR. you're an angel.
@srinivasjayaram570
@srinivasjayaram570 Жыл бұрын
The best explanation ever !
@rohitshrirame2378
@rohitshrirame2378 Жыл бұрын
understood!!!! thanks for all of these!!!!!!
@rishabhgupta9846
@rishabhgupta9846 Жыл бұрын
understood,great explanation
@anshumanbehera3706
@anshumanbehera3706 Жыл бұрын
understood thanks for the amazing Explations
@subhamoybera6456
@subhamoybera6456 Жыл бұрын
Great explanation 👍
@Code_JAVA268
@Code_JAVA268 4 ай бұрын
Kudos to you striver highly recommended 🎉❤
@1tav0
@1tav0 Жыл бұрын
awesome. thnk u striver understood
@mohmedfaizrozdarsyed1234
@mohmedfaizrozdarsyed1234 5 ай бұрын
Thank you sir , Really appreciate your effort
@stith_pragya
@stith_pragya 7 ай бұрын
Thank You So Much for this wonderful video.............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@chitranshjain5075
@chitranshjain5075 Жыл бұрын
Completely UNDERSTOOD
@ishangujarathi10
@ishangujarathi10 Жыл бұрын
amazing intuition!!! understood!!
@sripriyapotnuru5839
@sripriyapotnuru5839 Жыл бұрын
Thank you, Striver
@ParasLeela
@ParasLeela Жыл бұрын
it should be vis[nrow][ncol] = 2 instead of vis[nrow][ncol] = 1 otherwise we are never marking the fresh orange as rotten in the visited array
@jonascharles1686
@jonascharles1686 11 ай бұрын
Yep, he changed it later but didn't show that in video
@rohitkumarpilania94
@rohitkumarpilania94 Жыл бұрын
can be done withput the visited array, just keep rotting the oranges in grid by marking them as 2
@ankita7119
@ankita7119 Ай бұрын
but it is advised not to alter the question
@amalkumar256
@amalkumar256 11 ай бұрын
This series is amazing sir
@niranjanbhondave88
@niranjanbhondave88 Ай бұрын
I solved this question with a little bit of optimisation: while initialising queue, i maintained a fresh_count for number of fresh oranges. If grid[i][j] was 0 then visited[i][j] = 1, else if fresh orange then count++ else push in queue. each time i had a fresh orange, i marked visited ++ as well as count--. So to check for fresh oranges i didnt need another loop i just checked the count.If zero then only returned time else -1
@vikasbagri1225
@vikasbagri1225 Жыл бұрын
understood it very will...
@strickler453
@strickler453 Жыл бұрын
"understood" initially i thought : we can do without using extra space(true). but at the end : i got to know that don't lose the original input as company don't want to alter original data.
@PrimeContent01
@PrimeContent01 10 ай бұрын
understood this and all previous videos
@pervejmia8240
@pervejmia8240 Жыл бұрын
10 videos completed.. waiting for the next
@amansinghal4663
@amansinghal4663 Жыл бұрын
Just one correction bhaiya.... I think there was no need of using the max function for finding the time. Without it also the code gets accepted. You can find my AC below: class Solution { public: int bfs(queue&q, vector&visited, vector& grid, int rows, int columns){ int directions[5] = {0,-1,0,1,0}; int time=0; while(!q.empty()){ pairpr = q.front(); q.pop(); time = pr.second; for(int k=0; k=0 && nrow=0 && ncol
@elizabethr5161
@elizabethr5161 11 ай бұрын
Yes bro, you are right, since we use queue to store the time, there is no need of max variable.
@viratrunmachine815
@viratrunmachine815 Жыл бұрын
you are truly legend sir
@jatilyadav4000
@jatilyadav4000 Жыл бұрын
Best Explaination.....
@shubhamyadav2623
@shubhamyadav2623 Жыл бұрын
Just Amazing man !! I don't why all those idiots buys course if a guy like this giving us free !! We should support him for making such things free to us. Salute Striver🙋‍♀ good work bro!!
@EngineerPlays2024
@EngineerPlays2024 Жыл бұрын
Amazing vid man!
@_hulk748
@_hulk748 Жыл бұрын
Thankyou sir Understood 🙇‍♂❤🙏
@zishan53
@zishan53 Жыл бұрын
Understood I watch it on your codeBeyond using node and submitted correctly
@amanbhadani8840
@amanbhadani8840 Жыл бұрын
Hi bhaiya,can you tell me why you prefer to choose pairs in queue here,but in your old live classes you said that in interviews struct should be used due to its readibility.
@it_08amanagarwal35
@it_08amanagarwal35 Жыл бұрын
Love the series bro ❤❤❤❤
@shyren_more
@shyren_more Жыл бұрын
understood, thanks!
@pranjalnama2420
@pranjalnama2420 2 ай бұрын
Thanks amazing explanation
@DevashishJose
@DevashishJose 6 ай бұрын
understood,thank you.
@shivayadav-po8un
@shivayadav-po8un 2 ай бұрын
the major difference is when you see other tutorials, you can't write code until you see their code multiple times. In case of striver, his explanation is more than enough and our code just flows without even seeing striver's code. Take a bow#Striver
@ajayjangid1164
@ajayjangid1164 Жыл бұрын
You are Great🎉🎉 Bhaiya.. ❤❤
@sukritinarang5016
@sukritinarang5016 Жыл бұрын
Great Content !
@systempesystm
@systempesystm 3 күн бұрын
Loved This !!!
@nishanthcodes4576
@nishanthcodes4576 Жыл бұрын
understood!!!!!!!!!!!!!!!!!!!!! Thanks a lot
@GeniuslyTensai
@GeniuslyTensai Жыл бұрын
waiting for further amazing content
@anchalbudhrani572
@anchalbudhrani572 Жыл бұрын
Quality content 🤩
@Yash-uk8ib
@Yash-uk8ib Жыл бұрын
I think we do not have to maintain time at each step. Simplu counting levels in multisource bfs will do the job!
@ranjithduraisamy6868
@ranjithduraisamy6868 Жыл бұрын
Understood!! Thanks
@madhabkafle8898
@madhabkafle8898 Жыл бұрын
I think you should also say, how is this question related to previous one for better analysis .
@deepikasahu1592
@deepikasahu1592 Жыл бұрын
god level explanation!!
@ANURAGSINGH-nl2ll
@ANURAGSINGH-nl2ll 10 ай бұрын
understood thank you
@amarjeetkumar-hk2jl
@amarjeetkumar-hk2jl Жыл бұрын
Understood but please upload more videos as fast possible
@aysams2
@aysams2 Жыл бұрын
Sir please make a video on - Find the maximum of minimums of every window size. It is hard and important, but you haven't made a video about it yet.
@Amanjaisingh
@Amanjaisingh Жыл бұрын
cannt we do dfs for 1 and find the nearsest 2 bhi pasing int h in function which take the count if we go more deep recursively
@prashudeshmukh7902
@prashudeshmukh7902 Жыл бұрын
instead of pair........... we can use queue . keeping that aside ..amazing playlist bhaiya 👍👍
@vishvaschoudhary3858
@vishvaschoudhary3858 9 ай бұрын
or use queue q;
@UECAshutoshKumar
@UECAshutoshKumar 11 ай бұрын
Thank you sir
@VaibhavSingh-vy6gy
@VaibhavSingh-vy6gy Жыл бұрын
Rotten Oranges felt like a piece of cake😂.... thnx to striver
@harshkilaji1901
@harshkilaji1901 Жыл бұрын
Can we not instead find the nearest '2' or rotten orange for each '1' (fresh oranges) ?? And the answer would be the distance between farthest 2 and 1. If that '1' is unreachable we will automatically get -1. I guess we can also apply Dynamic programming to find subsequent distances.
@aayushbisht4307
@aayushbisht4307 Жыл бұрын
18:19 no need for maximum tm = t will work
@user-tk2vg5jt3l
@user-tk2vg5jt3l 3 ай бұрын
thank you Bhaiya
@nasreensaba8484
@nasreensaba8484 Жыл бұрын
Hi I am new to DSA, I would like to request you make a comprehensive video explaining what topics should I do and in what order in Arrays, Strings, Stacks, Queues.. GFG is huge and i cant figure out what to do and leave.
@takeUforward
@takeUforward Жыл бұрын
Coming tomorrow!
@akash-alam
@akash-alam Жыл бұрын
Your are awesome man.
@rahulsati5819
@rahulsati5819 Жыл бұрын
awesome yr ................
@herculean6748
@herculean6748 Жыл бұрын
Thanks striver!!
@CodeMode9313
@CodeMode9313 Жыл бұрын
Its awesome bhai
@dhyanprasad5611
@dhyanprasad5611 Жыл бұрын
OP OP oP OP OP, this series is FIRE !!
@morganyu3391
@morganyu3391 Жыл бұрын
Understood bhaiya
@priyanshajmera3177
@priyanshajmera3177 Жыл бұрын
Great explanation. But we don't need visited here so we can save some space.
@akarshsachan21
@akarshsachan21 Жыл бұрын
why we should take array instead of vector for the visited ?
G-11. Detect a Cycle in an Undirected Graph using BFS | C++ | Java
20:19
G-9. Flood Fill Algorithm | C++ | Java
20:34
take U forward
Рет қаралды 213 М.
Who has won ?? 😀 #shortvideo #lizzyisaeva
00:24
Lizzy Isaeva
Рет қаралды 64 МЛН
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 4,2 МЛН
Cool Items! New Gadgets, Smart Appliances 🌟 By 123 GO! House
00:18
123 GO! HOUSE
Рет қаралды 17 МЛН
Does size matter? BEACH EDITION
00:32
Mini Katana
Рет қаралды 20 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 363 М.
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
G-13. Distance of nearest cell having 1 | 0/1 Matrix | C++ | Java
20:21
❌ Don't Run Behind 500 LEETCODE Problems ❌ Focus on QPCD
8:31
Starting Competitive Programming - Steps and Mistakes
9:55
William Lin
Рет қаралды 1,4 МЛН
G-12. Detect a Cycle in an Undirected Graph using DFS | C++ | Java
19:10
Who has won ?? 😀 #shortvideo #lizzyisaeva
00:24
Lizzy Isaeva
Рет қаралды 64 МЛН