Rotten oranges problem | Leetcode
Vložit
- čas přidán 22. 03. 2020
- 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.com/SuryaPratapK/...
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??
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.😅
@@techdose4u okk sir, i got it :)
@@techdose4u Respect ✨
@@techdose4u You should apologize publically for such mistakes
🤣
Reaction when you read the probelm statement: "its so god damn hard"
After explanation: "So damn easy"
😂
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 😋
Welcome :)
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.
Thanks for supporting :)
Have you been placed yet? :)
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.
Awesome video, thanks for explaining it, in simple way.
This explaination makes a complex problem very easy to understand. Nice work @TECH DOSE. Keep doing the good work.
The best and the most intuitive explanation ever, thank you so much!
Welcome :)
Your explanation was so good and I understood perfectly. Thank You!!
This is the best explanation for this problem..such a great channel.
Thanks :)
Love your explanation, please keep up tha good work.
You are the hero we don't deserve,
But you are the hero we need.
...
😅 Thanks bro
Such an easy explanation, you have become my favorite youtuber
Thanks :)
Your explanations are so good! Thank you for all of your videos. Very, very helpful!!
Welcome 😊
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 :)
Subscribed!! I'm glad I find this channel
:)
Too good... Your channel's video are always on my top priority.... thanks god your are here to help....and also thanks to you....💥
😅Welcome
Great explanation. Would also be nice to see the concept implemented in code after the explanation.
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
@Tech Dose why this problem is not possible to solve with DFS. because it seems to be similar to search island problem.
So nice explanation
Quality content 😍😍😍.
Someone get this man an award
Tech Dose you are amazing. I can't watch anyone elses leetcode explanations
❤️
We can also do level order traversal where we keep track of the current level so that we wont have to maintain nodes.
👍🏼
Sharp & clear explanation!! Keep it up sir!
Thanks :)
You are helping students like us...tqs a lot... ❤️❤️❤️❤️
Welcome :)
Best Explanation till date!
Thanks :)
to the point explanation. thanks a lot!
Welcome :)
really a great explanation and also voice is very clear
Thanks
Sir please post more such leetcode problems .. they are very usefull
Many have requested for the same and so I decided to include leetcode problems as well. In future you will see many more.
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
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.
Best and clear explanation. Thanks
Welcome :)
you made the problem as easy as addition of two problems
Thanks 😅
@@techdose4u i will definitely donate and support our channel after i got placed
what a explanation! wow!
Thanks :)
thanks a lot, clear and precise explanation
Thanks
thank you sir v imp problem ❤️
Yea its very frequently asked :)
quality content free cost , god do exist. Hence prove
Thanks
an Awesome explanation😊😊
Thanks 😊
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) ??
By N i meant no of cells 😅 so N is actually NM because we need to travel all cells. Dint clearly mention that part.
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
What exactly?
Explanation of the code and a trace probably
This is God level explanation 🤩
😀
great explanation
Thanks 😊
Nice Explaination!!
Thanks :)
is space complexity O(1), because at the end we don't have any elements in the queue?
No, because we are using queue.
understood bhaiya thank your very much :)
Welcome
sir please also provide link for most optimized code i will able to write the code but thats nt most optimised
I am putting optimized codes from last 4 months. I will add from now on.
very nice explanation!
Thanks :)
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..
I don't think DFS will work because you need to process all cells with same timeframe simultaneously. Try submitting using BFS.
@@techdose4u ok.. how I ll understand which method(dfs/bfs) we have to use? This concept is not clear.
Thank you sir!!!
Welcome :)
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
Thanks a ton sir 👍
Welcome :)
Shouldn't the time complexity be O(m*n) ??
Awesome 🙏
Thanks
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.
Sir ,why we use delimeter ?? As in ur code u r using delimiter ..
Forgot the code compeltely 😅
thank you sir..
Welcome
brilliant
Thanks :)
Sir,Can you please explain the implementation of the code also from next time???
Sure
Thanks :)
Thank you
Welcome
👍🏾
Sir claps for you
Thanks
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
Sir can you provide the implementation code for this problem
Is it not present in link ?
Got it sir thanku
Inspired from Corona......
xD
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
👍🏼
Add the code too!
👍
Drop servicing == Dalaali returns true!!!