|
| 1 | +/* |
| 2 | +64. Minimum Path Sum |
| 3 | +Medium |
| 4 | +
|
| 5 | +Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path. |
| 6 | +Note: You can only move either down or right at any point in time. |
| 7 | +
|
| 8 | +Example 1: |
| 9 | +Input: grid = [[1,3,1],[1,5,1],[4,2,1]] |
| 10 | +Output: 7 |
| 11 | +Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum. |
| 12 | +
|
| 13 | +Example 2: |
| 14 | +Input: grid = [[1,2,3],[4,5,6]] |
| 15 | +Output: 12 |
| 16 | + |
| 17 | +Constraints: |
| 18 | +m == grid.length |
| 19 | +n == grid[i].length |
| 20 | +1 <= m, n <= 200 |
| 21 | +0 <= grid[i][j] <= 200 |
| 22 | +
|
| 23 | +*/ |
| 24 | + |
| 25 | +class Solution { |
| 26 | + static int[][] memo; |
| 27 | + public int minPathSum(int[][] grid) { |
| 28 | + int n = grid.length; |
| 29 | + int m = grid[0].length; |
| 30 | + |
| 31 | + memo =new int[n+1][m+1]; |
| 32 | + Arrays.stream(memo).forEach(a->Arrays.fill(a,-1)); |
| 33 | + // return solveRec(n-1,m-1,grid); |
| 34 | + //return solveTab(grid); |
| 35 | + return solveOpt(grid); |
| 36 | + } |
| 37 | + public int solveRec(int n,int m,int[][] grid){ |
| 38 | + //base case |
| 39 | + if(n==0 && m==0){ |
| 40 | + return grid[0][0]; |
| 41 | + } |
| 42 | + |
| 43 | + if(memo[n][m]!=-1){ |
| 44 | + return memo[n][m]; |
| 45 | + } |
| 46 | + |
| 47 | + // 2 choice either : |
| 48 | + //up and left : |
| 49 | + int up=Integer.MAX_VALUE,left=Integer.MAX_VALUE; |
| 50 | + if(n>0){ |
| 51 | + up = solveRec(n-1,m,grid); |
| 52 | + } |
| 53 | + if(m>0){ |
| 54 | + left = solveRec(n,m-1,grid); |
| 55 | + } |
| 56 | + return memo[n][m]=grid[n][m] + Math.min(left,up); |
| 57 | + } |
| 58 | + |
| 59 | + public int solveTab(int[][] grid){ |
| 60 | + int n = grid.length; |
| 61 | + int m = grid[0].length; |
| 62 | + int[][] tab = new int[n+1][m+1]; |
| 63 | + |
| 64 | + for(int i=0;i<n;i++){ |
| 65 | + for(int j=0;j<m;j++){ |
| 66 | + if(i==0 && j==0){ |
| 67 | + tab[i][j] = grid[i][j]; |
| 68 | + }else{ |
| 69 | + int up = (int)1e9,left =(int)1e9; |
| 70 | + if(i>0){ |
| 71 | + up = tab[i-1][j]; |
| 72 | + } |
| 73 | + if(j>0){ |
| 74 | + left = tab[i][j-1]; |
| 75 | + } |
| 76 | + tab[i][j] = grid[i][j]+Math.min(left,up); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return tab[n-1][m-1]; |
| 81 | + } |
| 82 | + public int solveOpt(int[][] grid){ |
| 83 | + int n = grid.length; |
| 84 | + int m = grid[0].length; |
| 85 | + int[] prev= new int[m+1]; |
| 86 | + |
| 87 | + |
| 88 | + for(int i=0;i<n;i++){ |
| 89 | + int[] temp = new int[m]; |
| 90 | + for(int j=0;j<m;j++){ |
| 91 | + if(i==0 && j==0){ |
| 92 | + temp[j] = grid[i][j]; |
| 93 | + }else{ |
| 94 | + int up = (int)1e9,left =(int)1e9; |
| 95 | + if(i>0){ |
| 96 | + up = prev[j]; |
| 97 | + } |
| 98 | + if(j>0){ |
| 99 | + left = temp[j-1]; |
| 100 | + } |
| 101 | + temp[j] = grid[i][j]+Math.min(left,up); |
| 102 | + } |
| 103 | + } |
| 104 | + prev = temp; |
| 105 | + } |
| 106 | + return prev[m-1]; |
| 107 | + } |
| 108 | +} |
0 commit comments