JAVA程式碼—演算法基礎:Minimum Path Sum問題
Minimum Path Sum問題
該問題來自 LeetCode 的第64題。
64. 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.
Note: You can only move either down or right at any point in time.
Example 1:
[[1,3,1],
[1,5,1],
[4,2,1]]
Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.
問題分析
該問題和另外一篇文章《 JAVA程式碼—演算法基礎:矩陣的最小路徑和問題》描述的其實是一類問題。連結:http://blog.csdn.net/seagal890/article/details/79590804 。都可以採用動態規劃(Dynamic Programming)演算法求解。
public int minPathSum(int[][] grid) {
int m = grid.length;// row
int n = grid[0].length; // column
for (int i = 0; i < m; i++) {
for (int j = 0 ; j < n; j++) {
if (i == 0 && j != 0) {
grid[i][j] = grid[i][j] + grid[i][j - 1];
} else if (i != 0 && j == 0) {
grid[i][j] = grid[i][j] + grid[i - 1][j];
} else if (i == 0 && j == 0) {
grid[i][j] = grid [i][j];
} else {
grid[i][j] = Math.min(grid[i][j - 1], grid[i - 1][j])
+ grid[i][j];
}
}
}
return grid[m - 1][n - 1];
}
簡化一下演算法:
public int minPathSum(int[][] grid) {
int m = grid.length;// row
int n = grid[0].length; // column
for(int j = 1; j < n; j++) grid[0][j] = grid[0][j] + grid[0][j-1];
for(int i = 1; i < m; i++) grid[i][0] = grid[i][0] + grid[i-1][0];
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
grid[i][j] = Math.min(grid[i][j - 1], grid[i - 1][j]) + grid[i][j];
}
}
return grid[m - 1][n - 1];
}
(完)
相關推薦
JAVA程式碼—演算法基礎:Minimum Path Sum問題
Minimum Path Sum問題 該問題來自 LeetCode 的第64題。 64. Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path
JAVA程式碼—演算法基礎:跳躍遊戲(II)
跳躍遊戲(Jump Game II) 問題描述: Given an array of non-negative integers, you are initially positioned at the first index of the array.
JAVA程式碼—演算法基礎:最少貨幣換錢問題求解(動態規劃)
最少貨幣換錢問題求解(動態規劃) 問題:換錢問題 給定一個數組arraydemo,arraydemo中所有的值都為正數且不重複。 每個值代表一種面值的貨幣,每種面值的貨幣可以使用任意張,再給定一個整數aim代表要找的錢數, 求組成aim的最少貨幣數
JAVA程式碼—演算法基礎:子陣列的最大累加和問題
子陣列的最大累加和問題 JAVA程式碼實現 【問題描述】 給定一個一維陣列,返回子陣列的最大累加和。 例如,arraydemo=[1,-2,3,5,-2,6,-1];所有子陣列中,[3,5,-2,
JAVA程式碼—演算法基礎:字謎分組
字謎分組 問題描述:給定一個字串陣列,請將陣列中的元素進行分組。例如,給定字串陣列為: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”],則返回結果為: [ [“ate”, “eat”,”tea”], [“na
JAVA程式碼—演算法基礎:數塔問題(動態規劃)
數塔問題(使用動態規劃思路求解) 如圖所示,給定一個正整數構成的三角形,如下所示: 在下面的數字三角形中尋找一條從頂部到底邊的路徑, 使得路徑上所經過的數字之和最大。 路徑上的每一步都只能往左下或者右下走。 只需要求出這個最大和即可,不必給出路徑。
【LeetCode-面試演算法經典-Java實現】【064-Minimum Path Sum(最小路徑和)】
原題 Given a m x n grid filled with non-negative numbers, find a path from top left to botto
【python】【leetcode】【演算法題目64—Minimum Path Sum】
題目描述 題目原文: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom
LeetCode算法系列:62. Unique Paths && 63. Unique Paths II && 64. Minimum Path Sum
這三個問題及其類似,背景都是和路徑相關,方法都是動態優化 目錄 62題: 題目描述: 演算法實現: 63題 題目描述: 演算法實現: 64題 題目描述 演算法實現 62題: 題目描述: A robot is located at the top-le
LeetCode:64. 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 o
Leetcode演算法——64、最小路徑之和(minimum path sum)
給定一個 m*n 的非負整數矩陣,找到一條路徑,從左上角到右下角,要求這條路徑上的所有陣列之和最小。 備註:每次只能向下或向右移動。 Input: [ [1,3,1], [1,5,1], [4,2,1] ] Output: 7 Explanation: Because
【LeetCode】064. Minimum Path Sum
ive rom right ott path sum 處理 tom ber its 題目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri
LeetCode 64. Minimum Path Sum 20170515
tco path sum obj .com 最小 ima minimum type elif Given a m x n grid filled with non-negative numbers, find a path from top left to bottom r
64. Minimum Path Sum
[] bottom blank prim from ini etc ++ i++ 題目: Given a m x n grid filled with non-negative numbers, find a path from top left to botto
LeetCode 64. Minimum Path Sum(最小和的路徑)
turn paths [0 solution 返回 資料 lan 需要 i++ Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right wh
Minimum Path Sum
problem from 邊界 move related quest sta maximum initial Given a m x n grid filled with non-negative numbers, find a path from top left to
[LintCode] Minimum Path Sum
down cti hash num mic tco code amp path sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom righ
最小路徑和 · Minimum Path Sum
情況 lex exit 結果 思路 其他 一句話 風格 number [抄題]: 給定一個只含非負整數的m*n網格,找到一條從左上角到右下角的可以使數字和最小的路徑。 [思維問題]: [一句話思路]: [輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況
leetcode64. Minimum Path Sum
turn ++ pre ini res nim leetcode class i++ DP class Solution { public: int minPathSum(vector<vector<int>>& grid) {
演算法基礎:資料型別,基礎結構(一)
基礎概念 一、資料型別 基本資料型別一般長度 (注意以下的 long long 實際上指的是 unsigned long long 型別) (long long 型別數值範圍是-9223372036854775808 ~ 9223372036854775807)差不多範圍是