Minimum Falling Path Sum | Recursion | Memo | Bottom Up | Leetcode 931

Поділитися
Вставка
  • Опубліковано 12 вер 2024
  • Whatsapp Community Link : www.whatsapp.c...
    This is the 8th Video of our Playlist "Dynami Programming : Popular Interview Problems".In this video we will try to solve a good and famous interview problem - Minimum Falling Path Sum (Leetcode 931)
    I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Also, please note that my Github solution link below contains both C++ as well as JAVA code.
    Problem Name : Minimum Falling Path Sum
    Company Tags : Google, Microsoft, Amazon, Flipkart, OLA, Goldman Sachs, MakeMyTrip, OYO Rooms, Samsung
    My solutions on Github(C++ & JAVA) : github.com/MAZ...
    Leetcode Link : leetcode.com/p...
    SImilar Qn : leetcode.com/p...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My GitHub Repo for interview preparation : github.com/MAZ...
    Subscribe to my channel : / @codestorywithmik
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Approach 1 Summary : The approach uses a recursive function MFS with memoization to compute the minimum sum by exploring possible paths from the top row to the bottom row of the matrix. The memoization is implemented using a separate 2D vector t to store previously computed results and avoid redundant calculations. The function iterates through neighboring elements in the next row and selects the path with the minimum sum. The main function then iterates over the first row and finds the minimum falling path sum by calling the recursive function for each starting column. The final result represents the minimum sum of a falling path through the matrix.
    Approach-2 Summary : The approach utilizes dynamic programming and a bottom-up approach to fill a 2D vector t with the minimum falling path sums for each element in the matrix. The initialization is done for the first row, and subsequent rows are filled by considering the minimum path sum from the previous row. The result is obtained by finding the minimum element in the last row of the matrix t. The solution efficiently computes the minimum falling path sum in a non-recursive manner, using dynamic programming to avoid redundant calculations.
    Approach-3 Summary : The approach utilizes a dynamic programming technique with constant space optimization. It maintains a 1D vector prev to store the minimum falling path sum for each column in the previous row. It iterates through the rows, updating the minimum falling path sum for each element in the current row (curr) based on the values in the prev vector. The prev vector is then updated with the values of the curr vector. Finally, the result is obtained by finding the minimum element in the last row of the matrix. The solution efficiently computes the minimum falling path sum with a reduced space complexity compared to a 2D matrix.
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    Timelines
    00:00 - Introduction
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #2024 #newyear

КОМЕНТАРІ • 107

  • @dileshchouhan9527
    @dileshchouhan9527 Рік тому +12

    The explanation & intution is fantastic :) , please make video on how did we think that when should we use dp , recursion etc all these method

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +3

      Thanks a lot Dilesh.
      Sure thing . Soon i will add playlists on concepts like those.
      Thanks again ❤️❤️❤️

  • @souravjoshi2293
    @souravjoshi2293 Рік тому +10

    You are the only person whose even long videos I can sit and watch patiently.
    Thanks a lot for this one too

  • @gui-codes
    @gui-codes 2 місяці тому

    i love how you focus only on intuition. For a beginner like me, i think it's the most important thing

  • @tarunsingh6374
    @tarunsingh6374 7 місяців тому +2

    Sir currently my exam is going on but I take time for daily leetcode question because of u . Some day I mis , but try continously.

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

    we can also further optimize the space complexity to O(n), by using 2 arrays. Bcz my next row is depending just on previous row.
    Nice explaination btw 🤘

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

      Indeed Pankaj. I have added this approach as well in my Github repo. It’s always good to explore multiple ways to solve. Great.
      Thank you ♥️

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

    So much to learn from this single video😊

  • @AnjuGupta-sz1uk
    @AnjuGupta-sz1uk 7 місяців тому +2

    As always, best solution and best explanation

  • @nilayjain9070
    @nilayjain9070 Місяць тому +1

    To avoid TLE in recursion + memo approach avoid using memset use two for loop to initialize the dp
    Also dont initialize dp with -1 (look at the constraints).
    My Accepted Code :-
    class Solution {
    public:
    int n;
    int dp[101][101];
    int solve(vector& matrix, int row, int col) {
    if (row == n) {
    return 0;
    }
    if (dp[row][col] != 2000) {
    return dp[row][col];
    }
    int right_down = INT_MAX, left_down = INT_MAX;
    int down = matrix[row][col] + solve(matrix, row + 1, col);
    if (col - 1 >= 0)
    left_down = matrix[row][col] + solve(matrix, row + 1, col - 1);
    if (col + 1 < n)
    right_down = matrix[row][col] + solve(matrix, row + 1, col + 1);
    return dp[row][col] = min({down, right_down, left_down});
    }
    int minFallingPathSum(vector& matrix) {
    n = matrix.size();
    int result = INT_MAX;
    for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 101; j++) {
    dp[i][j] = 2000;
    }
    }
    for (int j = 0; j < n; j++) {
    int a = solve(matrix, 0, j);
    result = min(result, a);
    }
    return result;
    }
    };
    💪

    • @mayank1522
      @mayank1522 27 днів тому

      can u send me the tabulation code for this

    • @nilayjain9070
      @nilayjain9070 27 днів тому

      @@mayank1522 I have not done this question with tabulation method

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

    bhai aapki coding videos netflix se zyada entertaining h maza aagya

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

      Thank you so much 😇🙏❤️
      One of the coolest comments I ever got 🤓

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

    doing great work bro :)

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

    bhai best ho yaar best mazaa aa gaya --- Love from maharashtra

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

      It means a lot. Thank you so much. 😇❤️🙏

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

    I applied a dynamic programming solution to solve it, skipping the recursive approach at first. Managed to do it on the first try with the DP solution.

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

    hey bro ... loving ur way of explanation. ❤

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

    👌🏻 such a dope explanation with less views. Extremely underrated.

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

    Sir, why greedy approach is working in the second approach? I mean every time , we are taking the smallest value and adding in the current sum. Please Explain. I am confusing in this because in the 1st approach, it is clear that this is DP question but in the second approach it seems like greedy question

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

    Perfect explaination! Thanks bhai🙌

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

    i always search for ur videos.... keep up the great work😊

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

    you are great - best solution ever
    🥳

  • @user-km4gv6uo9c
    @user-km4gv6uo9c 6 місяців тому

    bhaiya aapki video dekh kr maja aa gya ...please jo similar question hai uske liye bhi video bna do ...mujhe almost ek din sa hogya karte karte usko...solution yaad ho gya hai ...lekin samaj nhi aaya kisi ka youtube pr...(leetcode 1937)

  • @AnandKumar-kz3ls
    @AnandKumar-kz3ls Рік тому +2

    whats your time complexity for recursive + memoization solution ??

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

      Hi Anand, since we are eliminating to visit the already visited states, the time complexity is reduced to O(n*n)
      Would be glad to deep dive into it or go for a discussion more on this if required.

    • @AnandKumar-kz3ls
      @AnandKumar-kz3ls Рік тому +1

      @@codestorywithMIK your solve function is taking n*n time and we are looping for first row to find the minimum so time complexity would become O(n*n*n) ? ?

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

      My bad. You are right.
      Thanks for correction ❤️❤️❤️

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

    THanks a lot bhaiya the second approach's explanation was best ❤❤

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

    Thank you Sir! It was the best explanation i got.

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

      So glad to hear that Manash
      Thank you ❤️❤️❤️

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

    what is the reason its giving TLE in the 49th test case???
    class Solution {
    public:
    int n;
    int t[101][101];
    int solve(vector& matrix, int row, int col) {
    if (row == n - 1) {
    return matrix[row][col];
    }
    if (t[row][col] != -1) {
    return t[row][col];
    }
    int sum = matrix[row][col];
    int minSum = INT_MAX;
    if (row + 1 < n && col - 1 >= 0) {
    minSum = min(minSum, sum + solve(matrix, row + 1, col - 1));
    }
    if (row + 1 < n) {
    minSum = min(minSum, sum + solve(matrix, row + 1, col));
    }
    if (row + 1 < n && col + 1 < n) {
    minSum = min(minSum, sum + solve(matrix, row + 1, col + 1));
    }
    return t[row][col] = minSum;
    }
    int minFallingPathSum(vector& matrix) {
    n = matrix.size();
    memset(t, -1, sizeof(t));
    int row = 0;
    int result = 1e9;
    for (int col = 0; col < n; col++) {
    result = min(result, solve(matrix, row, col));
    }
    return result;
    }
    };

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

      Yes first approach now gives TLE. One new large test is added now.
      But in interviews, make sure to discuss this approach first.

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

      sure, thanks@@codestorywithMIK

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

    Bottom up kuch aise kiya tha par memoization ke baad bhi last test case mein TLE aa rha hai...
    class Solution
    {
    public://TRIAL
    int t[101][101];
    int row;
    int col;
    int solve(int i,int j,vector& matrix)
    {
    if(i>=row || j>=col || i

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

      You mean Top Down*
      . New test case is added.
      But there is one way to fix it. Fill all the values in dp array with INT_MAX
      Don’t use memset function to initialise the dp array. Do it with for loop.

    • @Pratibha-wf9qg
      @Pratibha-wf9qg 7 місяців тому

      hi @@codestorywithMIK how this will help? I am trying to figure out

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

    sir you are a gem aapse dp padhne ka bhut mann hai

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

      Thank you so much. Please follow my DP Concepts Playlist where I have started to explain from basics of DP - ua-cam.com/play/PLpIkg8OmuX-JhFpkhgrAwZRtukO0SkwAt.html&si=BdXiWDj5yTp82KMY

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

    2ns solution is mind blowing

  • @ritik6095
    @ritik6095 5 місяців тому

    hello! please explain the Ninja's training question of code studio, ts is variation of falling sum

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

    2nd approach was super good

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

    best explanation hai bhai thanks : - )

  • @Mohit-fe5hx
    @Mohit-fe5hx 7 місяців тому +1

    bhai Hats off You!!!

  • @thinkit5262
    @thinkit5262 7 місяців тому +2

    sir memo se 49/50 chal rhe TLE aa rha last case me

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

      Yes first approach now gives TLE. One new large test is added now.
      But in interviews, make sure to discuss this approach first.

  • @S3aw0lf
    @S3aw0lf 7 місяців тому +3

    The very first memoization code is failing because of the testcase containing all zeros n it's giving TLE. Could u explain why?

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

      Yes. New test case is added.
      But there is one way to fix it. Fill all the values in dp array with INT_MAX
      Don’t use memset function to initialise the dp array. Do it with for loop.

    • @AnshPr2311
      @AnshPr2311 3 місяці тому

      @@codestorywithMIK why this didn't give tle?

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

    intution is fantastic

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

    Best channel ever 🎉

  • @modiji8706
    @modiji8706 6 місяців тому

    Java Code
    class Solution {
    public int minFallingPathSum(int[][] matrix) {
    int max = Integer.MAX_VALUE;
    int n = matrix.length;
    int dp[][] = new int[n+1][n+1];
    for(int i =0;i

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

    recursion se kr rha jaise aap ne bataya hai pr last ke 3 cases me TLE aa raha hai same aap wala hi code hai
    Code
    Testcase
    Testcase
    Test Result
    All Submissions
    Time Limit Exceeded
    49 / 52 testcases passed
    Last Executed Input
    Use Testcase
    matrix =
    [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    View more
    Code
    C++
    class Solution {
    public:
    int t[101][101];int n;

    int solve(vector& matrix,int row,int col,int n)
    {
    int minsum=INT_MAX;
    int sum=matrix[row][col];
    if(row==n-1)
    {
    return matrix[row][col];
    }
    if(t[row][col]!=-1)
    {
    return t[row][col];
    }
    for(int shift=-1;shift=0 && col+shift

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

    Bhaiya min path sum -2 bhi isske ho jaayega kya??

  • @k-CE-OmkarPathak
    @k-CE-OmkarPathak 7 місяців тому

    👌

  • @NikhilSharma-qc1ez
    @NikhilSharma-qc1ez 7 місяців тому

    Nice explaination

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

    I didnt get the complexity of recursion+memoization?

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

    sir please share your leetcode profile

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

      Hi Aksha, you can google me as @Mazhar_MIK Leetcode”
      Thanks for watching 😇

  • @AmarjeetKumar-to9ub
    @AmarjeetKumar-to9ub 7 місяців тому +2

    🔥++

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

    Awesome 🙌

  • @anujK007
    @anujK007 7 місяців тому +2

    bhai mere wala tle de raha hai 49th tc pe memo wala code

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

      Yes. New test case is added.
      But there is one way to fix it. Fill all the values in dp array with INT_MAX
      Don’t use memset function to initialise the dp array. Do it with for loop.

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

      @@codestorywithMIK ya i did the same

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

    Bhai aapka code aakhri test case pe TLE de rha please update

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

      Yes first approach now gives TLE. One new large test is added now.
      But in interviews, make sure to discuss this approach first.

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

      @@codestorywithMIK could you update the code for the large test case

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

    Bhai mera memoization wla TLE de rha hai.
    code:
    class Solution {
    public:
    int n = 0;
    int t[101][101];
    int solve(vector& mat, int row, int col)
    {
    if(row == n-1)
    {
    return mat[row][col];
    }
    if(t[row][col] != -1)
    return t[row][col];
    int sum = mat[row][col];
    int minSum = INT_MAX;
    if(row+1 < n && col-1 >= 0)
    minSum = min(minSum, sum+solve(mat, row+1, col-1));
    if(row+1 < n && col < n)
    minSum = min(minSum, sum+solve(mat, row+1, col));
    if(row+1

  • @manab-l2d
    @manab-l2d 2 місяці тому

    pta nhi,mera memoise me tle kyu aa rha hai bar bar

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

    trying 1289. Minimum Falling Path Sum II

  • @AS-gf3ci
    @AS-gf3ci 7 місяців тому +1

    1st solution is giving me tle for the last 49th testcase. Is it happening to everyone?

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

      Yes. New test case is added.
      But there is one way to fix it. Fill all the values in memoization array with INT_MAX
      Can you please try and let me know if it works then.

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

      @@codestorywithMIK no,it's not working

    • @shraddhanandvikar8505
      @shraddhanandvikar8505 7 місяців тому +2

      ​@@anushkathakur6531 don`t use memset for initializing memoization array with INT_MAX . do it using normal 2 for loop
      CODE :
      class Solution {
      public:
      int t[101][101];
      int n;
      int solve(vector& matrix,int r,int c){
      int sum=matrix[r][c];
      if(r==n-1)
      return sum;
      if(t[r][c]!=INT_MAX)
      return t[r][c];
      int minsum=INT_MAX;
      if(r+1=0)
      minsum=min(minsum,sum+solve(matrix,r+1,c-1));
      if(r+1

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

      @@anushkathakur6531 use normal 2 for loop for filling all the values in memoization array with INT_MAX instead of using memset
      CPP Code :
      class Solution {
      public:
      int t[101][101];
      int n;
      int solve(vector& matrix,int r,int c){
      int sum=matrix[r][c];
      if(r==n-1)
      return sum;
      if(t[r][c]!=INT_MAX)
      return t[r][c];
      int minsum=INT_MAX;
      if(r+1=0)
      minsum=min(minsum,sum+solve(matrix,r+1,c-1));
      if(r+1

    • @AS-gf3ci
      @AS-gf3ci 7 місяців тому

      @@codestorywithMIK Yeah, it can also be solved by initializing the dp array with -101, that is outside the range.

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

    bhaiya phela solution kaam nhi kr rha, time limit exceeded aa rha.. idk why u didn't got the error.

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

      Can you share your code with me ?
      Are you using C++ ?

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

      45 out of 49 test cases are passing🥲

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

      Would like to see your code

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

      @codestorywithMIK
      Great explanation. Thanks a lot :)
      First solution is not working for me also. 49/50 test cases passed. Please find the code here:
      class Solution {
      public:
      int n;
      int dp[101][101];
      int solve(vector& matrix, int row, int col ){
      //Base condition
      if(row == n-1){
      return matrix[row][col];
      }
      if(dp[row][col] != -1)
      return dp[row][col];
      int res = INT_MAX;
      int sum = matrix[row][col];
      //explore all options in 3 directions
      vector arr = {-1, 0, 1};
      for(auto shift: arr){
      if(row >= 0 && row+1 =0 && col+shift

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

      @@ritupriya607 my code is almost same
      I am also getting tle 49/50 testcases :')

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

    O(1) space bhaiya I did
    class Solution {
    public:
    int minFallingPathSum(vector& matrix){
    if(matrix.size()==1)
    return matrix[0][0];
    int n=matrix.size();
    for(int i=1;i=0&&j+1=0&&j+1=0&&j-1>=0){
    matrix[i][j]+=min(matrix[i-1][j],matrix[i-1][j-1]);
    }
    }
    }
    return *min_element(matrix[n-1].begin(),matrix[n-1].end());
    }
    };
    O(1) space

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

    First code is giving TLE

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

      Yes first approach now gives TLE. One new large test is added now.
      But in interviews, make sure to discuss this approach first.

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

      Sure bhaiya@@codestorywithMIK

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

    the leetcode link has to be changed

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

    Bhaiya ye code galat output deraha h i think likha to sab shi h mene🥲
    class Solution {
    public:
    int minFallingPathSum(vector& matrix) {
    int n = matrix.size();
    vectorcopy(n, vector(n));
    //setting the first row of the new matrix
    for(int col = 0; col < n; col++){
    copy[0][col] = matrix[0][col];
    }

    for(int row = 1; row < n; row++){
    for(int col = 0; col < n; col++){
    int a = INT_MAX;
    int b = INT_MAX;
    if(col-1 >= 0)
    a = matrix[row-1][col-1];

    if(col+1 < n)
    b = matrix[row-1][col+1];

    copy[row][col] = matrix[row][col] + min({matrix[row-1][col], a, b});
    }
    }

    //return the min from the last row of copy
    int ans = INT_MAX;
    int last_row = n-1;
    for(int col = 0; col

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

      You are missing [row-1][col]
      Check that also.

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

      @@codestorywithMIK khaa pr bhaiyaa🥲, matrix[row-1][col] mene directly min function me daldiya