Leetcode 2435. Paths in Matrix Whose Sum Is Divisible by K |Dynamic Programming| Weekly Contest Hard

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

КОМЕНТАРІ • 38

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

    int mod = 1e9+7;
    int numberOfPaths(vector& grid, int k) {

    vectordp(grid.size(),
    vector(
    grid[0].size(),
    vector(51,-1)));
    return fnc(grid, k, 0, 0, 0, dp);
    }

    int fnc(vector& grid, int k, int i,int j, int sum,
    vector&dp)
    {
    //base conditions
    if(i=grid[0].size())
    {
    return 0;
    }
    //end cell of grid
    if(i==grid.size()-1 && j==grid[0].size()-1)
    {
    sum+=grid[i][j];
    if(sum%k==0)return 1;
    return 0;
    }

    if(dp[i][j][sum%k]!=-1)return dp[i][j][sum%k];
    long down = fnc(grid, k, i+1, j, sum+grid[i][j],dp); //down
    long right = fnc(grid, k, i, j+1, sum+grid[i][j], dp); //right
    return dp[i][j][sum%k] = (down%mod +right%mod)%mod;
    }

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

      i m beginner,is it good to start my coding journey direct from leetcode?

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

      @@myitraju7996 yaa absolutely

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

      @@yaswanthkosuru thanks 😊

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

      are didi java se karwao plss

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

    Please don't stop making videos on leetcode contests. Your explanation for contests is by far the best I have ever seen

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

    very well explained... i came across this question while searching for another similar question... can you please help solve..
    The Question is:- Given an array of integers of size N, count all possible distinct triplets whose sum is exactly divisible by given integer K. for triplets i, j, k --> i

  • @AbhishekKumar-qy1ek
    @AbhishekKumar-qy1ek 2 роки тому +2

    Thank you didi.
    Please make a video of third problem i.e using a robot to print the lexicographically Smallest string.

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

    HEY sis, i got placed at a company for 10lpa base. thank u so much for ur DSA videos . It helped me a lot

  • @AbhishekSingh-l9i
    @AbhishekSingh-l9i 3 місяці тому

    may god bless you and your channel

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

    if I don't understand from anywhere you always help me, always great explanation thank you

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

    Wonderful Explanation. Can you suggest a similar problem to 3D dp?

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

    Ma'am plzz Reply....
    Ma'am Aap all approaches btate ho
    And they are really good but for the popular question which tells us new approaches to aap uski approach khud se hi discover krte ho ya aapko bhi kayi baar dekhna hota h efficient approach.

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

      For example if we are new and solve snake and ladder problem toh khud se pura sochna muskil hota h
      same goes with special or popular questions which needs some different and new approach.

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

      Add a Reply plzz

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

      @@ayushsinghrathore360 bro , new approach khud se sochne ka try kro...bcoz Interviewer will tell u to optimize time and space complexity ......Pehle regular approach se solve kro....phir new approach try kro ...try for 20-30 mins ....then u may refer editorials or video solns if unable to solve through new or optimized approaches

    • @galibkiyaadein4627
      @galibkiyaadein4627 Рік тому +1

      Koi khud se nhi sochta kuch bro to be honest sab standard practice kar karke. Yddd mar lete hai

  • @AyushKumar-rh1vy
    @AyushKumar-rh1vy 2 роки тому

    Great explanation, Thanks for your effort 🔥

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

    Nice solution, just one request to please increase frequency of your videos

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

    Great job di

  • @Idukhan-jj9kc
    @Idukhan-jj9kc 2 роки тому

    Best solution Alisha 👌😉

  • @omkarkadam309
    @omkarkadam309 8 місяців тому

    "annnd listening" in the end is hilarious

  • @rishav144
    @rishav144 Рік тому +2

    pls dont stop uploading videos of leetcode questions😣

    • @probabilitycodingisfunis1
      @probabilitycodingisfunis1  Рік тому +1

      Will start soon again, thanks 👍

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

      @@probabilitycodingisfunis1 thanks ...your Leetcode videos are really helpful

  • @AmanSharma-vb5jl
    @AmanSharma-vb5jl 2 роки тому

    I don't have words to describe, only i can say "waah waah Kamal kr diya dhoti ko faad kr rumal kr diya"
    You are seriously awesome.

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

    Kudos to your hardwork

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

    Best solution

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

    You are returning 1 and 0 while question is asking total number of paths. Can u explain How they are getting added please.

    • @AbhishekPandey-yl3bp
      @AbhishekPandey-yl3bp 2 роки тому +1

      When you need the total count of something,you need to return 1 or 0 in the base case so it'll keep adding up to give the total count

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

    Maam continuosly getting rejected some times in tr ,hr, coding round how to handle this please help

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

    Fun fact 3rd one is harder than the 4th one waiting for 3 and welcome back 🥳

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

    You are best

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

    why do we need to memoize the rem ??
    i tried doing this using 2d dp but it didn't work. don't know where I'm going wrong
    int num(vector& grid, int &k,int i,int j,int sum,vector &dp){

    if(i

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

      @Mily, it will give wrong answer. consider the example
      [[1,2,3],[4,5,6],[7,8,9]] and k = 5,
      when you calculate for i=1,j=1 ,(grid[i][j]=5), for the path 1->2->5->8->9 gives sum=25 and sum%k==0 (because k=5), so you will store dp[i][j]=1 for this cell, so when you come to next function call in this cell at i=1,j=1 ,(grid[i][j]=5), for the path 1->2->5->6->9, the actual sum is 23, which means 23%5 !=0,
      but since you will not go to the complete path 1->2->5->6->9 using recursion, rather get your saved answer from dp[1][1] which tells you that it is 1 , you will stop there and consider answer to be 1 for this path also which is wrong,
      hence according to your code you will not get correct answer, you need to check for sum also ( new paths at each coordinate may have different sum than before which may not give answer)

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

      @@probabilitycodingisfunis1 understood, thank you so much

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

    this was great question! thanks for the explanation!
    And please accept my request on linkedin 😅