Rotten oranges problem | Leetcode

Sdílet
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/...

Komentáře • 148

  • @supernova7870
    @supernova7870 Před 4 lety +42

    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  Před 4 lety +85

      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 Před 4 lety +3

      @@techdose4u okk sir, i got it :)

    • @tarunkumar.d8379
      @tarunkumar.d8379 Před 2 lety +1

      @@techdose4u Respect ✨

    • @ashfaaqmir7869
      @ashfaaqmir7869 Před rokem

      @@techdose4u You should apologize publically for such mistakes

    • @pythontek
      @pythontek Před rokem

      🤣

  • @souravbarik8470
    @souravbarik8470 Před 3 lety +21

    Reaction when you read the probelm statement: "its so god damn hard"
    After explanation: "So damn easy"

  • @viralindian8326
    @viralindian8326 Před 4 lety +29

    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 😋

  • @shashikantkumar5095
    @shashikantkumar5095 Před 4 lety +29

    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.

  • @sushantpatial9819
    @sushantpatial9819 Před rokem +3

    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 :)

  • @divyagupta6854
    @divyagupta6854 Před 9 měsíci

    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.

  • @i_DeepakV
    @i_DeepakV Před 4 lety +1

    Awesome video, thanks for explaining it, in simple way.

  • @cutiex7357
    @cutiex7357 Před 2 lety

    Your explanation was so good and I understood perfectly. Thank You!!

  • @debaratighatak2211
    @debaratighatak2211 Před 2 lety +1

    The best and the most intuitive explanation ever, thank you so much!

  • @surbhitamrakar1638
    @surbhitamrakar1638 Před 3 lety +5

    This is the best explanation for this problem..such a great channel.

  • @rohannegi1330
    @rohannegi1330 Před 3 lety

    This explaination makes a complex problem very easy to understand. Nice work @TECH DOSE. Keep doing the good work.

  • @tianagreisel3923
    @tianagreisel3923 Před 2 lety +1

    Your explanations are so good! Thank you for all of your videos. Very, very helpful!!

  • @niveditha-7555
    @niveditha-7555 Před 3 lety +5

    Such an easy explanation, you have become my favorite youtuber

  • @dexkode5558
    @dexkode5558 Před 4 lety +3

    Subscribed!! I'm glad I find this channel

  • @TechBrain811
    @TechBrain811 Před 4 lety +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.
    ...

  • @ivanleon6164
    @ivanleon6164 Před 10 měsíci

    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.

  • @dikshamalik2565
    @dikshamalik2565 Před měsícem

    So nice explanation

  • @mohithkailash
    @mohithkailash Před rokem

    Someone get this man an award

  • @jordanwoltjer2024
    @jordanwoltjer2024 Před 2 lety

    Great explanation. Would also be nice to see the concept implemented in code after the explanation.

  • @jaswantrohila3776
    @jaswantrohila3776 Před 3 lety +1

    Too good... Your channel's video are always on my top priority.... thanks god your are here to help....and also thanks to you....💥

  • @niharikagrover3523
    @niharikagrover3523 Před 4 lety +1

    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

  • @vaibhavchawla2439
    @vaibhavchawla2439 Před 3 lety

    Quality content 😍😍😍.

  • @user-gd4lr1mt5x
    @user-gd4lr1mt5x Před 4 lety +1

    Best Explanation till date!

  • @eduardozevallos1509
    @eduardozevallos1509 Před 2 lety +1

    Tech Dose you are amazing. I can't watch anyone elses leetcode explanations

  • @ayanraj6295
    @ayanraj6295 Před 2 lety

    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

  • @ksquare1112
    @ksquare1112 Před 3 lety +1

    Best and clear explanation. Thanks

  • @sukritkapil2562
    @sukritkapil2562 Před 4 lety +1

    to the point explanation. thanks a lot!

  • @pusarlaaishwarya5035
    @pusarlaaishwarya5035 Před 3 lety +2

    You are helping students like us...tqs a lot... ❤️❤️❤️❤️

  • @prakharagarwal6237
    @prakharagarwal6237 Před 3 lety +1

    We can also do level order traversal where we keep track of the current level so that we wont have to maintain nodes.

  • @tushar6286
    @tushar6286 Před 3 lety +1

    Sharp & clear explanation!! Keep it up sir!

  • @yashpreetbathla4653
    @yashpreetbathla4653 Před 4 lety +4

    thank you sir v imp problem ❤️

    • @techdose4u
      @techdose4u  Před 4 lety +1

      Yea its very frequently asked :)

  • @ANKURSingh-yl2lj
    @ANKURSingh-yl2lj Před 3 lety +3

    really a great explanation and also voice is very clear

  • @keshavtawari2979
    @keshavtawari2979 Před 3 lety +2

    thanks a lot, clear and precise explanation

  • @deepjyotsinghkapoor1955
    @deepjyotsinghkapoor1955 Před 4 lety +5

    what a explanation! wow!

  • @ash_engineering
    @ash_engineering Před 4 lety +2

    Sir please post more such leetcode problems .. they are very usefull

    • @techdose4u
      @techdose4u  Před 4 lety +1

      Many have requested for the same and so I decided to include leetcode problems as well. In future you will see many more.

  • @rajivsarkar277
    @rajivsarkar277 Před 3 lety

    @Tech Dose why this problem is not possible to solve with DFS. because it seems to be similar to search island problem.

  • @rahulkumarbarik7584
    @rahulkumarbarik7584 Před 3 lety +7

    quality content free cost , god do exist. Hence prove

  • @ashishverma1382
    @ashishverma1382 Před 2 lety +2

    great explanation

  • @arunmozhichelvanscse7349
    @arunmozhichelvanscse7349 Před 2 lety +2

    an Awesome explanation😊😊

  • @E__ShameemAhamedS
    @E__ShameemAhamedS Před 3 lety +1

    you made the problem as easy as addition of two problems

    • @techdose4u
      @techdose4u  Před 3 lety

      Thanks 😅

    • @E__ShameemAhamedS
      @E__ShameemAhamedS Před 3 lety

      @@techdose4u i will definitely donate and support our channel after i got placed

  • @manasvinsharma1740
    @manasvinsharma1740 Před 3 lety +1

    This is God level explanation 🤩

  • @prateeksinghal630
    @prateeksinghal630 Před 4 lety +1

    Nice Explaination!!

  • @prachurjyabasistha4682
    @prachurjyabasistha4682 Před 4 lety +1

    very nice explanation!

  • @hardiknahata4328
    @hardiknahata4328 Před 4 lety +1

    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

  • @v.sreesairam9488
    @v.sreesairam9488 Před 3 lety +1

    understood bhaiya thank your very much :)

  • @srutiverma6977
    @srutiverma6977 Před 3 lety +2

    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

  • @nknidhi321
    @nknidhi321 Před 3 lety +1

    Awesome 🙏

  • @abhishekgautam1063
    @abhishekgautam1063 Před 4 lety +3

    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  Před 4 lety +2

      By N i meant no of cells 😅 so N is actually NM because we need to travel all cells. Dint clearly mention that part.

  • @nitin5865
    @nitin5865 Před 3 lety +1

    Thanks a ton sir 👍

  • @revarevanth1800
    @revarevanth1800 Před 3 lety +1

    Thank you sir!!!

  • @peeyooshmanitiwari9082
    @peeyooshmanitiwari9082 Před 3 lety +1

    thank you sir..

  • @Od253
    @Od253 Před 4 lety

    👍🏾

  • @Shuvooa
    @Shuvooa Před 4 lety +1

    brilliant

  • @sandeeprajurs1994
    @sandeeprajurs1994 Před 3 lety +1

    is space complexity O(1), because at the end we don't have any elements in the queue?

  • @prashantverma3347
    @prashantverma3347 Před 4 lety

    Thank you

  • @spetsnaz_2
    @spetsnaz_2 Před 4 lety

    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

  • @sohinidey6711
    @sohinidey6711 Před 4 lety +1

    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  Před 4 lety

      I don't think DFS will work because you need to process all cells with same timeframe simultaneously. Try submitting using BFS.

    • @sohinidey6711
      @sohinidey6711 Před 4 lety

      @@techdose4u ok.. how I ll understand which method(dfs/bfs) we have to use? This concept is not clear.

  • @lakshayv3272
    @lakshayv3272 Před 3 lety +1

  • @hapysethi1306
    @hapysethi1306 Před 4 lety +1

    Sir claps for you

  • @AhmadRazaUnique
    @AhmadRazaUnique Před 3 lety

    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

  • @sagarksahoo4667
    @sagarksahoo4667 Před 2 lety

    Shouldn't the time complexity be O(m*n) ??

  • @aakashgoswami2356
    @aakashgoswami2356 Před 3 lety

    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.

  • @hrushikeshpatil3772
    @hrushikeshpatil3772 Před 4 lety +2

    Inspired from Corona......

  • @reetverma6876
    @reetverma6876 Před 4 lety +1

    Sir ,why we use delimeter ?? As in ur code u r using delimiter ..

  • @ANKURSingh-yl2lj
    @ANKURSingh-yl2lj Před 3 lety +2

    sir please also provide link for most optimized code i will able to write the code but thats nt most optimised

    • @techdose4u
      @techdose4u  Před 3 lety

      I am putting optimized codes from last 4 months. I will add from now on.

  • @konakandlarohith6681
    @konakandlarohith6681 Před 3 lety +1

    Sir,Can you please explain the implementation of the code also from next time???

  • @adarshsharadpandey4763

    Drop servicing == Dalaali returns true!!!

  • @pragyasingh9543
    @pragyasingh9543 Před 3 lety +1

    Sir can you provide the implementation code for this problem

  • @bathininipun9797
    @bathininipun9797 Před 3 lety +1

    Add the code too!