forked from 1ramagrawal0610/h4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumPathSum.java
More file actions
39 lines (35 loc) · 1001 Bytes
/
MinimumPathSum.java
File metadata and controls
39 lines (35 loc) · 1001 Bytes
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
package dp;
import java.util.Arrays;
public class MinimumPathSum {
public static void main(String[] args) {
int m=3,n=3;
int[][] dp=new int[m][n];
int[][] path=new int[][] {{1,3,1},
{1,5,1},
{4,2,1}};
for(int i=0;i<m;i++)Arrays.fill(dp[i], -1);
System.out.println(solve(m-1, n-1, dp,path));
System.out.println(solve1(m,n,path));
}
public static int solve(int m,int n,int[][] dp,int[][] path) {
if(m==0 && n==0)return path[0][0];
if(m<0||n<0)return Integer.MAX_VALUE;
if(dp[m][n]!=-1)return dp[m][n];
return dp[m][n]=path[m][n]+Math.min(solve(m-1,n,dp,path),solve(m,n-1,dp,path));
}
public static int solve1(int m,int n,int[][] path) {
int[][] dp=new int[m][n];
dp[0][0]=path[0][0];
for(int i=0;i<m;i++) {
for(int j=0;j<n;j++) {
if(i==0 && j==0)continue;
int x=Integer.MAX_VALUE;
int y=Integer.MAX_VALUE;
if(i>0)x=dp[i-1][j];
if(j>0)y=dp[i][j-1];
dp[i][j]=path[i][j]+Math.min(x, y);
}
}
return dp[m-1][n-1];
}
}