Rotten oranges problem | Leetcode

  Рет қаралды 64,303

Techdose

Techdose

Күн бұрын

This video explains a very frequently asked programming interview question which is to find the time taken to rot all oranges in a basket of orange. This video explains the most efficient way to solve this problem with example. Time complexity of the algorithm is O(N) which is the best time for this problem. CODE LINK is given below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
CODE LINK: gist.github.co...

Пікірлер: 150
@supernova7870
@supernova7870 4 жыл бұрын
Sir, at the starting of the program we are required to put all the rotten oranges in the queue and for that we have to look entire 2d array to get those oranges , so time complexity is O(N^2) at the starting only and you said it will be O(N). PlZz clear it??
@techdose4u
@techdose4u 4 жыл бұрын
I wrote O(N) thinking N = No of cells. No of cells = Row*Cols. So basically TIME is O(N*M). Sometimes I miss to say certain details.😅
@supernova7870
@supernova7870 4 жыл бұрын
@@techdose4u okk sir, i got it :)
@tarunkumar.d8379
@tarunkumar.d8379 3 жыл бұрын
@@techdose4u Respect ✨
@ashfaaqmir7869
@ashfaaqmir7869 2 жыл бұрын
@@techdose4u You should apologize publically for such mistakes
@pythontek
@pythontek Жыл бұрын
🤣
@viralindian8326
@viralindian8326 4 жыл бұрын
By watching your two videos of rotten oranges and number of islands...I am feeling comfortable with Graph theory of BFS and DFS now... Thanks dude 😋
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@shashikantkumar5095
@shashikantkumar5095 4 жыл бұрын
Thank you, Sir, for providing premium level videos free of cost to us, After getting placed I would like to donate and support this channel.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks for supporting :)
@rohithreddy5201
@rohithreddy5201 3 ай бұрын
Have you been placed yet? :)
@divyagupta6854
@divyagupta6854 Жыл бұрын
Sir, you explained the algorithm so well, and I was able to code it properly too!! Just two edge cases need to be considered while coding this algorithm, that when we have rotten orange at edge of the grid anywhere, we might check a fresh orange in out of bounds of grid, so we have to also check that coordinate's x - 1, or coordinate's y - 1, or coordinate's x + 1, or coordinate's y + 1 must be in index range of grid. And we also have to keep a global variable to regularly update it whenever timestamp is being increased, so that, even though our queue gets empty and we won't have access of its last element, we have the last updated value of timestamp globally.
@souravbarik8470
@souravbarik8470 4 жыл бұрын
Reaction when you read the probelm statement: "its so god damn hard" After explanation: "So damn easy"
@techdose4u
@techdose4u 4 жыл бұрын
😂
@sushantpatial9819
@sushantpatial9819 2 жыл бұрын
Alternatively, we can keep count of all the fresh oranges while adding the indices of rotten oranges to the queue. While checking for all the indices in the queue, we will decrease the fresh oranges count if we encounter a 1. In the end if our fresh oranges count is greater than 0, that means we still have fresh oranges left in the basked and we can return -1 :)
@surbhitamrakar1638
@surbhitamrakar1638 3 жыл бұрын
This is the best explanation for this problem..such a great channel.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@TechBrain811
@TechBrain811 4 жыл бұрын
Love your explanation, please keep up tha good work. You are the hero we don't deserve, But you are the hero we need. ...
@techdose4u
@techdose4u 4 жыл бұрын
😅 Thanks bro
@niharikagrover3523
@niharikagrover3523 4 жыл бұрын
Could you explain where we should use BFS and in which scenarios we can used DFS, I know that will come from practice but still a video explaining this concept will be helpful
@ivanleon6164
@ivanleon6164 Жыл бұрын
an easier way to check if all oranges are being rotten(IMO) is at the initial scan just count how many oranges are there(1 or 2), then when adding an element to the queue you can count how many you have added, if those 2 counters are the same, then all oranges were rotten at the end.
@abhishekgautam1063
@abhishekgautam1063 4 жыл бұрын
Number of rows are N and let M be the number of columns. Also we'll be traversing the whole grid so will the complexity be O(N*M) ??
@techdose4u
@techdose4u 4 жыл бұрын
By N i meant no of cells 😅 so N is actually NM because we need to travel all cells. Dint clearly mention that part.
@debaratighatak2211
@debaratighatak2211 3 жыл бұрын
The best and the most intuitive explanation ever, thank you so much!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@rahulkumarbarik7584
@rahulkumarbarik7584 4 жыл бұрын
quality content free cost , god do exist. Hence prove
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@rohannegi1330
@rohannegi1330 3 жыл бұрын
This explaination makes a complex problem very easy to understand. Nice work @TECH DOSE. Keep doing the good work.
@jordanwoltjer2024
@jordanwoltjer2024 2 жыл бұрын
Great explanation. Would also be nice to see the concept implemented in code after the explanation.
@jaswantrohila3776
@jaswantrohila3776 3 жыл бұрын
Too good... Your channel's video are always on my top priority.... thanks god your are here to help....and also thanks to you....💥
@techdose4u
@techdose4u 3 жыл бұрын
😅Welcome
@ANKURSingh-yl2lj
@ANKURSingh-yl2lj 4 жыл бұрын
really a great explanation and also voice is very clear
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@ash_engineering
@ash_engineering 4 жыл бұрын
Sir please post more such leetcode problems .. they are very usefull
@techdose4u
@techdose4u 4 жыл бұрын
Many have requested for the same and so I decided to include leetcode problems as well. In future you will see many more.
@arunmozhichelvanscse7349
@arunmozhichelvanscse7349 2 жыл бұрын
an Awesome explanation😊😊
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@ayanraj6295
@ayanraj6295 3 жыл бұрын
Thanks for such a nice explanation. Here's my easy implementation: class Solution { public: int orangesRotting(vector& grid) { vectortime(grid.size(),vector(grid[0].size(),0)); vectorvisited(grid.size(),vector(grid[0].size(),0)); int ans=0; queueq; for(int i=0;i
@pusarlaaishwarya5035
@pusarlaaishwarya5035 4 жыл бұрын
You are helping students like us...tqs a lot... ❤️❤️❤️❤️
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@ashishverma1382
@ashishverma1382 2 жыл бұрын
great explanation
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@mma-dost
@mma-dost Ай бұрын
is this the optimized code. Its somewhat looks like bruteforce. We are using space for storing all those nodes.
@rajivsarkar277
@rajivsarkar277 3 жыл бұрын
@Tech Dose why this problem is not possible to solve with DFS. because it seems to be similar to search island problem.
@tianagreisel3923
@tianagreisel3923 2 жыл бұрын
Your explanations are so good! Thank you for all of your videos. Very, very helpful!!
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😊
@srutiverma6977
@srutiverma6977 3 жыл бұрын
class Solution { public: //Function to find minimum time required to rot all oranges. struct Node{ int t; int r,c; }; int orangesRotting(vector& grid) { // Code here queueq; Node* node; int n=grid.size(),m=grid[0].size(),count; for(int i=0;ir=i; node->c=j; q.push(node);} while(!q.empty()){ Node* a=q.front(); q.pop(); // coutr,j=a->c,ti=a->t; count=ti; if(0r=i+1; node->c=j; q.push(node);} if(0r=i; node->c=j+1; q.push(node);} if(0r=i-1; node->c=j; q.push(node);} if(0r=i; node->c=j-1; q.push(node);} } for(int i=0;i
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@yashpreetbathla4653
@yashpreetbathla4653 4 жыл бұрын
thank you sir v imp problem ❤️
@techdose4u
@techdose4u 4 жыл бұрын
Yea its very frequently asked :)
@deepjyotsinghkapoor1955
@deepjyotsinghkapoor1955 4 жыл бұрын
what a explanation! wow!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@i_DeepakV
@i_DeepakV 4 жыл бұрын
Awesome video, thanks for explaining it, in simple way.
@tushar6286
@tushar6286 3 жыл бұрын
Sharp & clear explanation!! Keep it up sir!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@dikshamalik2565
@dikshamalik2565 4 ай бұрын
So nice explanation
@cutiex7357
@cutiex7357 3 жыл бұрын
Your explanation was so good and I understood perfectly. Thank You!!
@konakandlarohith6681
@konakandlarohith6681 3 жыл бұрын
Sir,Can you please explain the implementation of the code also from next time???
@techdose4u
@techdose4u 3 жыл бұрын
Sure
@sagarksahoo4667
@sagarksahoo4667 2 жыл бұрын
Shouldn't the time complexity be O(m*n) ??
@manasvinsharma1740
@manasvinsharma1740 3 жыл бұрын
This is God level explanation 🤩
@techdose4u
@techdose4u 3 жыл бұрын
😀
@ksquare1112
@ksquare1112 4 жыл бұрын
Best and clear explanation. Thanks
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@aakashgoswami2356
@aakashgoswami2356 3 жыл бұрын
Sir i have 2 doubt : 1.Sir in your code ,in line 61 ,what is the purpose of writing temp.x = -1 ,temp.y = -1 and i dont't understand delimeter part. 2. My second doubt is when i submit code on leetcode ,it is showing heap buffer overflow on address... plzz solve above 2 problems asap.
@reetverma6876
@reetverma6876 4 жыл бұрын
Sir ,why we use delimeter ?? As in ur code u r using delimiter ..
@techdose4u
@techdose4u 4 жыл бұрын
Forgot the code compeltely 😅
@keshavtawari2979
@keshavtawari2979 4 жыл бұрын
thanks a lot, clear and precise explanation
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@ANKURSingh-yl2lj
@ANKURSingh-yl2lj 4 жыл бұрын
sir please also provide link for most optimized code i will able to write the code but thats nt most optimised
@techdose4u
@techdose4u 4 жыл бұрын
I am putting optimized codes from last 4 months. I will add from now on.
@hrushikeshpatil3772
@hrushikeshpatil3772 4 жыл бұрын
Inspired from Corona......
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
xD
@nknidhi321
@nknidhi321 3 жыл бұрын
Awesome 🙏
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@v.sreesairam9488
@v.sreesairam9488 3 жыл бұрын
understood bhaiya thank your very much :)
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@prateeksinghal630
@prateeksinghal630 4 жыл бұрын
Nice Explaination!!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@prachurjyabasistha4682
@prachurjyabasistha4682 4 жыл бұрын
very nice explanation!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@pragyasingh9543
@pragyasingh9543 4 жыл бұрын
Sir can you provide the implementation code for this problem
@techdose4u
@techdose4u 4 жыл бұрын
Is it not present in link ?
@pragyasingh9543
@pragyasingh9543 4 жыл бұрын
Got it sir thanku
@NinjaCoders
@NinjaCoders Ай бұрын
bhai code kaha hai ?
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
Awesome explanation! This is a simple implementation of the above explanation:::::: int orangesRotting(vector& grid) { if(grid.empty()) { return 0; } int counter{0}; queue q; for(auto row=0; row
@Shuvooa
@Shuvooa 4 жыл бұрын
brilliant
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@lakshayv3272
@lakshayv3272 3 жыл бұрын
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@revarevanth1800
@revarevanth1800 4 жыл бұрын
Thank you sir!!!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@adarshsharadpandey4763
@adarshsharadpandey4763 Жыл бұрын
Drop servicing == Dalaali returns true!!!
@prashantverma3347
@prashantverma3347 4 жыл бұрын
Thank you
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@niveditha-7555
@niveditha-7555 4 жыл бұрын
Such an easy explanation, you have become my favorite youtuber
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@dexkode5558
@dexkode5558 4 жыл бұрын
Subscribed!! I'm glad I find this channel
@techdose4u
@techdose4u 4 жыл бұрын
:)
@bathininipun9797
@bathininipun9797 3 жыл бұрын
Add the code too!
@techdose4u
@techdose4u 3 жыл бұрын
👍
@sandeeprajurs1994
@sandeeprajurs1994 3 жыл бұрын
is space complexity O(1), because at the end we don't have any elements in the queue?
@techdose4u
@techdose4u 3 жыл бұрын
No, because we are using queue.
@sohinidey6711
@sohinidey6711 4 жыл бұрын
Can I use dfs? I traverse every element in the matrix and use dfs for every element if the element is 2.. time complexity will be O(m*n) . Will that process be right?? we don't need to check for element value 1 if top, down,left,right any element is 2 or not...we will keep one variable which will store Maximum time frame..
@techdose4u
@techdose4u 4 жыл бұрын
I don't think DFS will work because you need to process all cells with same timeframe simultaneously. Try submitting using BFS.
@sohinidey6711
@sohinidey6711 4 жыл бұрын
@@techdose4u ok.. how I ll understand which method(dfs/bfs) we have to use? This concept is not clear.
@eduardozevallos1509
@eduardozevallos1509 2 жыл бұрын
Tech Dose you are amazing. I can't watch anyone elses leetcode explanations
@techdose4u
@techdose4u 2 жыл бұрын
❤️
@hardiknahata4328
@hardiknahata4328 4 жыл бұрын
Explanation was good, but I lack skills when transforming ideas to code. Hope you will cover that in future videos. Most of the programming tutorials lack that. Just my views. Thanks a lot
@techdose4u
@techdose4u 4 жыл бұрын
What exactly?
@hardiknahata4328
@hardiknahata4328 4 жыл бұрын
Explanation of the code and a trace probably
@prakharagarwal6237
@prakharagarwal6237 3 жыл бұрын
We can also do level order traversal where we keep track of the current level so that we wont have to maintain nodes.
@techdose4u
@techdose4u 3 жыл бұрын
👍🏼
@nitin5865
@nitin5865 3 жыл бұрын
Thanks a ton sir 👍
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@E__ShameemAhamedS
@E__ShameemAhamedS 3 жыл бұрын
you made the problem as easy as addition of two problems
@techdose4u
@techdose4u 3 жыл бұрын
Thanks 😅
@E__ShameemAhamedS
@E__ShameemAhamedS 3 жыл бұрын
@@techdose4u i will definitely donate and support our channel after i got placed
@AhmadRazaUnique
@AhmadRazaUnique 3 жыл бұрын
Sir, there may be -1 is the answer when we have some neighbor 1 to 1. Its not for sure that the answer will be other than -1 when we dont have any 1 sorrounded by 1 or 2.. Please clearify sir
@peeyooshmanitiwari9082
@peeyooshmanitiwari9082 4 жыл бұрын
thank you sir..
@techdose4u
@techdose4u 4 жыл бұрын
Welcome
@sukritkapil2562
@sukritkapil2562 4 жыл бұрын
to the point explanation. thanks a lot!
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@hapysethi1306
@hapysethi1306 4 жыл бұрын
Sir claps for you
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@2018-o2p
@2018-o2p 4 жыл бұрын
Best Explanation till date!
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@mohithkailash
@mohithkailash 2 жыл бұрын
Someone get this man an award
@vaibhavchawla2439
@vaibhavchawla2439 4 жыл бұрын
Quality content 😍😍😍.
@Od253
@Od253 4 жыл бұрын
👍🏾
Topological sort | Course schedule 2 | Leetcode #210
12:51
Techdose
Рет қаралды 91 М.
Rotting Oranges - Leetcode 994 - Graphs (Python)
16:09
Greg Hogg
Рет қаралды 2,3 М.
Incredible: Teacher builds airplane to teach kids behavior! #shorts
00:32
Fabiosa Stories
Рет қаралды 10 МЛН
From Small To Giant Pop Corn #katebrush #funny #shorts
00:17
Kate Brush
Рет қаралды 69 МЛН
Крутой фокус + секрет! #shorts
00:10
Роман Magic
Рет қаралды 19 МЛН
G-10. Rotten Oranges | C++ | Java
22:30
take U forward
Рет қаралды 318 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 231 М.
Rotting Oranges | Directional Arrays & BFS | LeetCode
14:55
AlgosWithMichael
Рет қаралды 4,6 М.
Rotting Oranges - Leetcode 994 - Python
12:19
NeetCode
Рет қаралды 102 М.
Remove K digits | Build lowest number | Leetcode #402
15:30
Techdose
Рет қаралды 89 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 364 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 546 М.
Rotting Oranges
15:44
Kevin Naughton Jr.
Рет қаралды 64 М.
Incredible: Teacher builds airplane to teach kids behavior! #shorts
00:32
Fabiosa Stories
Рет қаралды 10 МЛН