Rotten oranges problem | Leetcode

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 150

  • @supernova7870
    @supernova7870 4 роки тому +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  4 роки тому +87

      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 роки тому +3

      @@techdose4u okk sir, i got it :)

    • @tarunkumar.d8379
      @tarunkumar.d8379 3 роки тому +1

      @@techdose4u Respect ✨

    • @ashfaaqmir7869
      @ashfaaqmir7869 2 роки тому

      @@techdose4u You should apologize publically for such mistakes

    • @pythontek
      @pythontek 2 роки тому

      🤣

  • @souravbarik8470
    @souravbarik8470 4 роки тому +22

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

  • @viralindian8326
    @viralindian8326 4 роки тому +32

    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 4 роки тому +30

    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.

  • @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.

  • @sushantpatial9819
    @sushantpatial9819 2 роки тому +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 :)

  • @surbhitamrakar1638
    @surbhitamrakar1638 3 роки тому +5

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

  • @niveditha-7555
    @niveditha-7555 4 роки тому +5

    Such an easy explanation, you have become my favorite youtuber

  • @debaratighatak2211
    @debaratighatak2211 3 роки тому +1

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

  • @TechBrain811
    @TechBrain811 4 роки тому +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.
    ...

  • @arunmozhichelvanscse7349
    @arunmozhichelvanscse7349 2 роки тому +2

    an Awesome explanation😊😊

  • @mohithkailash
    @mohithkailash 2 роки тому

    Someone get this man an award

  • @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.

  • @srutiverma6977
    @srutiverma6977 3 роки тому +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

  • @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

  • @jaswantrohila3776
    @jaswantrohila3776 4 роки тому +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....💥

  • @eduardozevallos1509
    @eduardozevallos1509 3 роки тому +1

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

  • @prakharagarwal6237
    @prakharagarwal6237 3 роки тому +1

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

  • @ashishverma1382
    @ashishverma1382 3 роки тому +2

    great explanation

  • @rahulkumarbarik7584
    @rahulkumarbarik7584 4 роки тому +8

    quality content free cost , god do exist. Hence prove

  • @tianagreisel3923
    @tianagreisel3923 3 роки тому +1

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

  • @pusarlaaishwarya5035
    @pusarlaaishwarya5035 4 роки тому +2

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

  • @E__ShameemAhamedS
    @E__ShameemAhamedS 4 роки тому +1

    you made the problem as easy as addition of two problems

    • @techdose4u
      @techdose4u  4 роки тому

      Thanks 😅

    • @E__ShameemAhamedS
      @E__ShameemAhamedS 4 роки тому

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

  • @dexkode5558
    @dexkode5558 4 роки тому +3

    Subscribed!! I'm glad I find this channel

  • @rohannegi1330
    @rohannegi1330 4 роки тому

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

  • @ANKURSingh-yl2lj
    @ANKURSingh-yl2lj 4 роки тому +3

    really a great explanation and also voice is very clear

  • @intuitive_coder
    @intuitive_coder 4 роки тому +1

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

  • @tushar6286
    @tushar6286 3 роки тому +1

    Sharp & clear explanation!! Keep it up sir!

  • @2018-o2p
    @2018-o2p 4 роки тому +1

    Best Explanation till date!

  • @ash_engineering
    @ash_engineering 4 роки тому +2

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

    • @techdose4u
      @techdose4u  4 роки тому +1

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

  • @manasvinsharma1740
    @manasvinsharma1740 3 роки тому +1

    This is God level explanation 🤩

  • @ksquare1112
    @ksquare1112 4 роки тому +1

    Best and clear explanation. Thanks

  • @deepjyotsinghkapoor1955
    @deepjyotsinghkapoor1955 4 роки тому +5

    what a explanation! wow!

  • @abhishekgautam1063
    @abhishekgautam1063 4 роки тому +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  4 роки тому +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.

  • @niharikagrover3523
    @niharikagrover3523 4 роки тому +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

  • @dikshamalik2565
    @dikshamalik2565 7 місяців тому

    So nice explanation

  • @yashpreetbathla4653
    @yashpreetbathla4653 4 роки тому +4

    thank you sir v imp problem ❤️

    • @techdose4u
      @techdose4u  4 роки тому +1

      Yea its very frequently asked :)

  • @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.

  • @cutiex7357
    @cutiex7357 3 роки тому

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

  • @jordanwoltjer2024
    @jordanwoltjer2024 2 роки тому

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

  • @mma-dost
    @mma-dost 4 місяці тому

    is this the optimized code. Its somewhat looks like bruteforce. We are using space for storing all those nodes.

  • @keshavtawari2979
    @keshavtawari2979 4 роки тому +2

    thanks a lot, clear and precise explanation

  • @hardiknahata4328
    @hardiknahata4328 4 роки тому +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

  • @prateeksinghal630
    @prateeksinghal630 4 роки тому +1

    Nice Explaination!!

  • @vaibhavchawla2439
    @vaibhavchawla2439 4 роки тому

    Quality content 😍😍😍.

  • @sukritkapil2562
    @sukritkapil2562 4 роки тому +1

    to the point explanation. thanks a lot!

  • @nknidhi321
    @nknidhi321 3 роки тому +1

    Awesome 🙏

  • @sandeeprajurs1994
    @sandeeprajurs1994 3 роки тому +1

    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.

  • @prachurjyabasistha4682
    @prachurjyabasistha4682 4 роки тому +1

    very nice explanation!

  • @v.sreesairam9488
    @v.sreesairam9488 3 роки тому +1

    understood bhaiya thank your very much :)

  • @konakandlarohith6681
    @konakandlarohith6681 3 роки тому +1

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

  • @sohinidey6711
    @sohinidey6711 4 роки тому +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  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.

  • @ANKURSingh-yl2lj
    @ANKURSingh-yl2lj 4 роки тому +2

    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.

  • @nitin5865
    @nitin5865 3 роки тому +1

    Thanks a ton sir 👍

  • @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

  • @reetverma6876
    @reetverma6876 4 роки тому +1

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

    • @techdose4u
      @techdose4u  4 роки тому

      Forgot the code compeltely 😅

  • @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

  • @pragyasingh9543
    @pragyasingh9543 4 роки тому +1

    Sir can you provide the implementation code for this problem

  • @lakshayv3272
    @lakshayv3272 3 роки тому +1

  • @hrushikeshpatil3772
    @hrushikeshpatil3772 4 роки тому +2

    Inspired from Corona......

  • @Shuvooa
    @Shuvooa 4 роки тому +1

    brilliant

  • @peeyooshmanitiwari9082
    @peeyooshmanitiwari9082 4 роки тому +1

    thank you sir..

  • @revarevanth1800
    @revarevanth1800 4 роки тому +1

    Thank you sir!!!

  • @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.

  • @sagarksahoo4667
    @sagarksahoo4667 2 роки тому

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

  • @hapysethi1306
    @hapysethi1306 4 роки тому +1

    Sir claps for you

  • @bathininipun9797
    @bathininipun9797 3 роки тому +1

    Add the code too!

  • @NinjaCoders
    @NinjaCoders 4 місяці тому

    bhai code kaha hai ?

  • @prashantverma3347
    @prashantverma3347 4 роки тому

    Thank you

  • @Padhai_Vadhai_PV
    @Padhai_Vadhai_PV Рік тому

    Drop servicing == Dalaali returns true!!!

  • @Od253
    @Od253 4 роки тому

    👍🏾