zoukankan      html  css  js  c++  java
  • [LintCode] Minimum Path Sum

    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.

    You can only move either down or right at any point in time.

    Solution 1. Recursion, DFS 

     1 public class Solution {
     2     private int minPath = Integer.MAX_VALUE;
     3     public int minPathSum(int[][] grid) {
     4         if(grid == null || grid.length == 0 || grid[0].length == 0){
     5             return -1;
     6         }
     7         traverse(grid, 0, 0, 0);
     8         return minPath;
     9     }
    10     private void traverse(int[][] grid, int x, int y, int sum){
    11         int m = grid.length, n = grid[0].length;
    12         if(x >= m || y >= n) {
    13             return;
    14         }
    15         sum += grid[x][y];
    16         if(x == m - 1 && y == n - 1){
    17             if(sum < minPath){
    18                 minPath = sum;
    19             }
    20             return;
    21         }
    22         traverse(grid, x + 1, y, sum);  
    23         traverse(grid, x, y + 1, sum); 
    24     }
    25 }

    Solution 2. DFS, divide and conquer + memoization

     1 public class Solution {
     2     private int[][] hash;
     3     public int minPathSum(int[][] grid) {
     4         if(grid == null || grid.length == 0 || grid[0].length == 0){
     5             return -1;
     6         }
     7         hash = new int[grid.length][grid[0].length];
     8         for(int i = 0; i < hash.length; i++){
     9             for(int j = 0; j < hash[0].length; j++){
    10                 hash[i][j] = Integer.MAX_VALUE;
    11             }
    12         }
    13         return divideConquer(grid, 0, 0);
    14     }
    15     private int divideConquer(int[][] grid, int x, int y){
    16         int m = grid.length, n = grid[0].length;
    17         if(x == m - 1 && y == n - 1){
    18             return grid[x][y];
    19         }
    20         if(x >= m || y >= n){
    21             return Integer.MAX_VALUE;
    22         }
    23         if(hash[x][y] != Integer.MAX_VALUE){
    24             return hash[x][y];
    25         }
    26         hash[x][y] = grid[x][y] + Math.min(divideConquer(grid, x, y + 1), divideConquer(grid, x + 1, y));
    27         return hash[x][y];
    28     }
    29 }

    Solution 3.  Dynamic Programming 

    State: f[i][j],  the min distance from (0, 0) to (i, j)

    Function: f[i][j] = grid[i][j] + Math.min(f[i - 1][j], f[i][j - 1])

    Initialization: f[i][0] = grid[i][0] + f[i - 1][0]; f[0][j] = grid[0][j] + f[0][j - 1]

    Answer: f[m - 1][n - 1]

     1 public class Solution {
     2     public int minPathSum(int[][] grid) {
     3         if(grid == null || grid.length == 0 || grid[0].length == 0){
     4             return -1;
     5         }
     6         int rowLen = grid.length, colLen = grid[0].length;
     7         int[][] f = new int[rowLen][colLen];
     8         f[0][0] = grid[0][0];
     9         for(int row = 1; row < rowLen; row++){
    10             f[row][0] = grid[row][0] + f[row - 1][0]; 
    11         }
    12         for(int col = 1; col < colLen; col++){
    13             f[0][col] = grid[0][col] + f[0][col - 1];
    14         }
    15         for(int i = 1; i < rowLen; i++){
    16             for(int j = 1; j < colLen; j++){
    17                 f[i][j] = grid[i][j] + Math.min(f[i][j - 1], f[i - 1][j]);
    18             }
    19         }
    20         return f[rowLen - 1][colLen - 1];
    21     }
    22 }

    Solution 4. DP with space optimization

     1 public class Solution {
     2     public int minPathSum(int[][] grid) {
     3         if(grid == null || grid.length == 0 || grid[0].length == 0){
     4             return -1;
     5         }
     6         int rowLen = grid.length, colLen = grid[0].length;
     7         int[][] T = new int[2][colLen];
     8         T[0][0] = grid[0][0];
     9         for(int j = 1; j < colLen; j++){
    10             T[0][j] = T[0][j - 1] + grid[0][j];
    11         }
    12         for(int i = 1; i < rowLen; i++){
    13             T[i % 2][0] = T[(i - 1) % 2][0] + grid[i][0];
    14             for(int j = 1; j < colLen; j++){
    15                 T[i % 2][j] = grid[i][j] + Math.min(T[(i - 1) % 2][j], T[i % 2][j - 1]);
    16             }
    17         }
    18         return T[(rowLen - 1) % 2][colLen - 1];
    19     }
    20 }

    Related Problems

    Triangle 

    Binary Tree Maximum Path Sum 

  • 相关阅读:
    hibernate对应的annocation版本
    Struts 2 OGNL
    Struts2的Stack Context和ValueStack
    Struts2中的OGNL详解
    struts2中根对象以及ognl .
    在Struts 2中实现IoC
    Struts2的属性驱动与模型驱动的区别
    Struts2的模型驱动
    Java中线程的锁和数据库中的事务隔离级别
    为什么socket编程要用到多线程
  • 原文地址:https://www.cnblogs.com/lz87/p/7498455.html
Copyright © 2011-2022 走看看