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??
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.
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 :)
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.
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
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
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
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
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;
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..
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, 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
🤣
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 :)
Reaction when you read the probelm statement: "its so god damn hard"
After explanation: "So damn easy"
😂
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.
This is the best explanation for this problem..such a great channel.
Thanks :)
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 :)
Such an easy explanation, you have become my favorite youtuber
Thanks :)
Someone get this man an award
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
The best and the most intuitive explanation ever, thank you so much!
Welcome :)
Tech Dose you are amazing. I can't watch anyone elses leetcode explanations
❤️
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.
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
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
This explaination makes a complex problem very easy to understand. Nice work @TECH DOSE. Keep doing the good work.
You are helping students like us...tqs a lot... ❤️❤️❤️❤️
Welcome :)
Your explanations are so good! Thank you for all of your videos. Very, very helpful!!
Welcome 😊
really a great explanation and also voice is very clear
Thanks
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 Awesome explanation😊😊
Thanks 😊
Awesome video, thanks for explaining it, in simple way.
Subscribed!! I'm glad I find this channel
:)
Your explanation was so good and I understood perfectly. Thank You!!
We can also do level order traversal where we keep track of the current level so that we wont have to maintain nodes.
👍🏼
quality content free cost , god do exist. Hence prove
Thanks
Sharp & clear explanation!! Keep it up sir!
Thanks :)
Best Explanation till date!
Thanks :)
This is God level explanation 🤩
😀
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.
Great explanation. Would also be nice to see the concept implemented in code after the explanation.
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
Best and clear explanation. Thanks
Welcome :)
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
👍🏼
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
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.
great explanation
Thanks 😊
So nice explanation
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 😍😍😍.
to the point explanation. thanks a lot!
Welcome :)
understood bhaiya thank your very much :)
Welcome
very nice explanation!
Thanks :)
Nice Explaination!!
Thanks :)
@Tech Dose why this problem is not possible to solve with DFS. because it seems to be similar to search island problem.
is this the optimized code. Its somewhat looks like bruteforce. We are using space for storing all those nodes.
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
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 please explain the implementation of the code also from next time???
Sure
Awesome 🙏
Thanks
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.
Sir ,why we use delimeter ?? As in ur code u r using delimiter ..
Forgot the code compeltely 😅
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.
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.
Inspired from Corona......
xD
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.
Shouldn't the time complexity be O(m*n) ??
Thanks a ton sir 👍
Welcome :)
Sir can you provide the implementation code for this problem
Is it not present in link ?
Got it sir thanku
Thank you sir!!!
Welcome :)
brilliant
Thanks :)
bhai code kaha hai ?
Drop servicing == Dalaali returns true!!!
thank you sir..
Welcome
Add the code too!
👍
Sir claps for you
Thanks
Thank you
Welcome
👍🏾