Minimum Falling Path Sum II in C++

C++Server Side ProgrammingProgramming

Suppose we have a grid arr, this is a square grid, a falling path with non-zero shifts is a choice of exactly one element from each row of arr, such that no two elements chosen in adjacent rows are present in the same column. We have to find the minimum sum of a falling path with nonzero shifts.

So, if the input is like arr is like [[1,2,3],[4,5,6],[7,8,9]], then the output will be 13, as there are different falling paths, these are like [1,5,9], [1,5,7], [1,6,7], [1,6,8], [2,4,8], [2,4,9], [2,6,7], [2,6,8], [3,4,8], [3,4,9], [3,5,7], [3,5,9]. Now the falling path with the smallest sum is [1,5,7], so the answer is 13.

To solve this, we will follow these steps −

  • n := number of rows, m := number of columns

  • for initialize i := 1, when i < n, update (increase i by 1), do −

    • Define arrays leftMin and rightMin of size m

    • leftMin[0] := arr[i - 1, 0]

    • for initialize j := 1, when j < m, update (increase j by 1), do −

      • leftMin[j] := minimum of leftMin[j - 1] and arr[i - 1, j]

    • rightMin[m - 1] = arr[i - 1, m - 1]

    • for initialize j := m - 2, when j >= 0, update (decrease j by 1), do −

      • rightMin[j] := minimum of arr[i - 1, j] and rightMin[j + 1]

    • for initialize j := 0, when j < m, update (increase j by 1), do −

      • leftVal := (if (j - 1) >= 0, then leftMin[j - 1], otherwise 1000000)

      • rightVal := (if (j + 1) < m, then rightMin[j + 1], otherwise 1000000)

      • arr[i, j] := arr[i, j] + min(leftVal, rightVal)

  • ans := inf

  • for initialize i := 0, when i < m, update (increase i by 1), do −

    • ans := minimum of ans and arr[n - 1, i]

  • return ans

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int dp[10005][205];
class Solution {
   public:
   void pre(){
      for(int i = 0; i <= 10000; i++){
         for(int j = 0; j <=204; j++){
            dp[i][j] = -1;
         }
      }
   }
   int minFallingPathSum(vector<vector<int>>& arr) {
      int n = arr.size();
      int m = arr[0].size();
      for (int i = 1; i < n; i++) {
         vector<int> leftMin(m);
         vector<int> rightMin(m);
         leftMin[0] = arr[i - 1][0];
         for (int j = 1; j < m; j++) {
            leftMin[j] = min(leftMin[j - 1], arr[i - 1][j]);
         }
         rightMin[m - 1] = arr[i - 1][m - 1];
         for (int j = m - 2; j >= 0; j--) {
            rightMin[j] = min(arr[i - 1][j], rightMin[j + 1]);
         }
         for (int j = 0; j < m; j++) {
            int leftVal = (j - 1) >= 0 ? leftMin[j - 1] :
            1000000;
            int rightVal = (j + 1) < m ? rightMin[j + 1] :
            1000000;
            arr[i][j] += min(leftVal, rightVal);
         }
      }
      int ans = INT_MAX;
      for (int i = 0; i < m; i++)
      ans = min(ans, arr[n - 1][i]);
      return ans;
   }
};
main(){
   Solution ob;
   vector<vector<int>> v = {{1,2,3},{4,5,6},{7,8,9}};
   cout << (ob.minFallingPathSum(v));
}

Input

{{1,2,3},{4,5,6},{7,8,9}}

Output

13
raja
Published on 08-Jun-2020 14:32:17
Advertisements