-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminfallpathsum.java
More file actions
61 lines (55 loc) · 1.7 KB
/
Copy pathminfallpathsum.java
File metadata and controls
61 lines (55 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution {
public int minFallingPathSum(int[][] matrix) {
int dp[][]=new int[matrix.length][matrix[0].length];
for (int j = 0; j < matrix[0].length; j++) {
dp[0][j] = matrix[0][j];
}
for(int i=1;i<matrix.length;i++)
{
for(int j=0;j<matrix[0].length;j++)
{
int left = (j > 0) ? matrix[i][j] + dp[i - 1][j - 1]:(int)1e9;
int up = matrix[i][j] + dp[i - 1][j];
int right = (j < matrix[0].length - 1) ? matrix[i][j] + dp[i - 1][j + 1]: (int)1e9 ;
dp[i][j] = Math.min(left, Math.min(up, right));
}
}
int minPathSum = Integer.MAX_VALUE;
for (int j = 0; j < matrix[0].length; j++) {
minPathSum = Math.min(minPathSum, dp[matrix.length - 1][j]);
}
return minPathSum;
}
}
/*
for(int temp[]:dp)
{
Arrays.fill(temp,-1);
}
for(int i=0;i<matrix[0].length;i++)
{
a=Math.min(a,findans(matrix,0,i,dp));
}
return a;
}
public static int findans(int mat[][],int i,int j,int dp[][])
{
if(i>=mat.length || j >=mat[0].length || j<0)
{
return (int)1e9;
}
if(i==mat.length-1 && j>=0)
{
return mat[i][j];
}
if(dp[i][j]!=-1)
{
return dp[i][j];
}
int ans=Integer.MAX_VALUE;
int left=mat[i][j]+findans(mat,i+1,j-1,dp);
int down=mat[i][j]+findans(mat,i+1,j,dp);
int right=mat[i][j]+findans(mat,i+1,j+1,dp);
ans=Math.min(left,Math.min(down,right));
return dp[i][j]=ans;
*/