[InterviewBit] Min Sum Path in Triangle

Min Sum Path in Triangle

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12

int helper(vector<vector<int>>& A, vector<vector<int>>& dp, int r, int n, int c) {
if(r == n) return 0;
if(dp[r][c] != -1) return dp[r][c];
return dp[r][c] = A[r][c] + min(helper(A,dp,r + 1, n, c), helper(A,dp,r + 1, n, c + 1));
}
int Solution::minimumTotal(vector<vector<int> > &A) {
int n = A.size();
vector<vector<int>> dp(n,vector<int>(n,-1));
return helper(A,dp,0,n,0);
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/13/PS/interviewbit/min-sum-path-in-triangle/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.