20150708 lintcode 总结 Minimum Path Sum

原创 2015年07月08日 15:09:06

Easy Minimum Path Sum

34%
Accepted

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.

虽然是简单的题,感觉也不简单

Time complexity: O(m*n); Space complexity: O(m*n)

public class Solution {
    /**
     * @param grid: a list of lists of integers.
     * @return: An integer, minimizes the sum of all numbers along its path
     */
    public int minPathSum(int[][] grid) {
        // write your code here
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int m = grid.length;
        int n = grid[0].length;
        int[][] temp = new int[m][n];
        
        for(int i = 0; i<m ; i++){      	
        	for(int j = 0; j<n; j++){
        		if(i==0 && j==0){
        			temp[0][0] = grid[0][0];
        		}else if(i==0){
            		temp[0][j] = temp[0][j-1] + grid[i][j];
            	}else if(j==0){
            		temp[i][0] = temp[i-1][0] + grid[i][j];
            	}else{
            	    temp[i][j] = Math.min(temp[i-1][j], temp[i][j-1]) + grid[i][j];
            	}      		
    	   }
       }
        
<span style="white-space:pre">	</span>return temp[m-1][n-1];
    }
}

方法2:time complexity: O(m*n); space complexity: O(n)

public class Solution {
    /**
     * @param grid: a list of lists of integers.
     * @return: An integer, minimizes the sum of all numbers along its path
     */
    public int minPathSum(int[][] grid) {
        // write your code here
    	if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
    	int m = grid.length;
        int n = grid[0].length;
        int[] temp = new int[n];
        
        for(int i = 0; i<m; i++){     
        	for(int j = 0; j<n; j++){
        		if(i == 0 && j == 0 ){
        			temp[0] = grid[0][0];
        		}else if(i == 0 ){
        			temp[j] = temp[j-1] + grid[0][j];
        		}else if(j == 0 ){
        			temp[0] = temp[0] + grid[i][0];
        		}else{
        			temp[j] = Math.min(temp[j], temp[j-1]) + grid[i][j];
        		}	
        	}       		
       }      
        return temp[n-1];
    }
}


相关文章推荐

20150708 lintcode 总结 Binary Tree Maximum Path Sum Show result *****

Medium Binary Tree Maximum Path Sum Show result  23% Accepted Given a binary tree, find the ...

20150708 lintcode 总结 Binary Search Tree Iterator

Binary Search Tree Iterator Design an iterator over a binary search tree with the following rul...

动态规划--最小路径和(Minimum Path Sum)

原题   Given a m x n grid filled with non-negative numbers, find a path from top left to bottom rig...

Leetcode64 Minimum Path Sum

Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to...

leetcode[63] Minimum Path Sum 动态规划

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which...

LeetCode刷题(C++)——Minimum Path Sum(Medium)

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which...

LeetCode 1.Minimum Path Sum 2.Unique Paths I and II

大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths  A robot is located a...

LeetCode: Minimum Path Sum [063]

【题目】 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom righ...

leetcode笔记:Minimum Path Sum

题目的大意是,给定一个m*n的网格,每个格子里有一个非负整数,找到一条从左上角到右下角的路径,使其经过的格子数值之和最小,每一步只能向右或向下走。可使用动态规划来解决该问题。...

leetcode Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:20150708 lintcode 总结 Minimum Path Sum
举报原因:
原因补充:

(最多只允许输入30个字)