Cherry Pickup | BackTracking Solution | Leetcode 741

Поділитися
Вставка
  • Опубліковано 12 вер 2024
  • Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
    NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. For a better experience and more exercises, VISIT: www.pepcoding....
    Have a look at our result: www.pepcoding....
    Follow us on our UA-cam page: / pepcoding
    Follow us on our FB page: / pepcoding
    Follow us on Instagram: / pepcoding
    Follow us on LinkedIn: / pepcoding-education
    Follow us on Pinterest: / _created
    Follow us on Twitter: home
    .
    .
    .
    Happy Programming !!! Pep it up 😍🤩
    .
    .
    .
    #pepcoding #code #coder #codinglife #programming #coding #java #freeresources #datastrucutres #pepcode #competitive #competitiveprogramming #softwareengineer #engineering #engineer

КОМЕНТАРІ • 25

  • @abhishekupadhayay9399
    @abhishekupadhayay9399 3 роки тому +11

    No one can imagine, how much happy I m after seeing sir back on UA-cam,
    Just see him calmness while explaining the solution...
    Sir is really a legend🔥

  • @dayanandraut5660
    @dayanandraut5660 2 роки тому +5

    Time complexity analysis. For going from top left to bottom right. O(2^nxn) and for each such path, we need to explore 2^nxn paths. so overal time complexity will be O(4^nxn). i.e 4 to the power n square

    • @AnshulSharma-gq2vn
      @AnshulSharma-gq2vn Рік тому

      I think the overall time complexity should be O(4 ^ (n + n)) because in any path you will at most traverse through n rows and n columns before reaching your destination.

  • @AbhishekMishra-me4mu
    @AbhishekMishra-me4mu 2 роки тому +2

    If the input is [[1]]. This code will give 0 as the answer! So include one more condition to handle this specific input, rest inputs works fine! Nice Explanation though!!

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

    here is the cpp solution
    //backtracking approach - TLE
    class Solution {
    public:
    int maxi=0;
    void help2(int r,int c,vector&grid,int count,int n)
    {
    if(r=n || c=n || grid[r][c]==-1)
    return;
    if(r==0 && c==0)
    {
    maxi=max(maxi,count);
    return;
    }
    int cherry=grid[r][c];
    grid[r][c]=0;
    help2(r-1,c,grid,count+cherry,n);
    help2(r,c-1,grid,count+cherry,n);
    grid[r][c]=cherry;
    }
    void help(int r,int c,vector&grid,int count,int n)
    {
    if(r=n || c=n || grid[r][c]==-1)
    return;
    if(r==n-1 && c==n-1)
    {
    int cher=grid[r][c];
    grid[r][c]=0;
    help2(r,c,grid,count+cher,n);
    grid[r][c]=cher;
    return;
    }
    int cherry=grid[r][c];
    grid[r][c]=0;
    help(r+1,c,grid,count+cherry,n);
    help(r,c+1,grid,count+cherry,n);
    grid[r][c]=cherry;
    }
    int cherryPickup(vector& grid) {
    // int count=0;
    int n=grid.size();
    help(0,0,grid,0,n);
    return maxi;
    }
    };

  • @B_i_n_o_d
    @B_i_n_o_d 3 роки тому +6

    I think it will not work for one corner case,grid= [[1]]

    • @gautamarora6556
      @gautamarora6556 3 роки тому +3

      Add this condition in before making call the any other function:
      if(grid.size()== 1 && grid[0][0]== 1) return 1;

  • @samyakjain7422
    @samyakjain7422 2 роки тому +1

    You are definitely real life Jeetu bhaiya as in reel life Jeetu bhaiya in Kota Factory.The way you explained approach and solution, I became your fan.

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

      Thanks for your compliment.
      Keep learning.
      And for better experience, visit nados.io, where you will get well curated content and career opportunities.

  • @KandulaAsrith
    @KandulaAsrith 7 місяців тому +1

    very good explanation sir. Thank you...

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

    the base case -:
    row>=n || col >=n || ar[row][col]==-1 is sufficient

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

      For better insight, visit nados.io, post your doubts, community will help you out there.

  • @shantanukumar4081
    @shantanukumar4081 2 роки тому +1

    Quality Content Great Explanation !!!

  • @NikhilKumar-vf9nv
    @NikhilKumar-vf9nv 3 роки тому

    Thank you sir for making these good level questions vedios this question asked by many companies in OA as well as in interviews

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

    Good to see you sir after a long time. We were missing you.

  • @Mercer80
    @Mercer80 2 роки тому +1

    no dry run or even a recursion tree to show the working of code ,where is the backtracking done ? anyone plz explain

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

    time complexcity discuss krdo ek baar it would be good

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

    Great explanation

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

    Bhaiya Maximum Product Subarray pe please bnai video. Abhi tak nhi bani hai

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

    Can this question be solved through recursion also?

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

      bhai recursion hi toh he ye bus isme backtracking aur aa gayi he

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

      this can be more optimized using dp

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

    bhaiya plz add all dp videos in dp playlist level 2 on youtube

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

    bhaiya please do leetcode 78 if possilbe

  • @261288888
    @261288888 2 роки тому +1

    Bahut slow ho aap