Skip to content

Commit 6e91830

Browse files
committed
solve problem Minimum Path Sum
1 parent 593a89b commit 6e91830

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ All solutions will be accepted!
242242
|328|[Odd Even Linked List](https://leetcode-cn.com/problems/odd-even-linked-list/description/)|[java/py/js](./algorithms/OddEvenLinkedList)|Medium|
243243
|677|[Map Sum Pairs](https://leetcode-cn.com/problems/map-sum-pairs/description/)|[java/py/js](./algorithms/MapSumPairs)|Medium|
244244
|442|[Find All Duplicates In An Array](https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/description/)|[java/py/js](./algorithms/FindAllDuplicatesInAnArray)|Medium|
245+
|64|[Minimum Path Sum](https://leetcode-cn.com/problems/minimum-path-sum/description/)|[java/py/js](./algorithms/MinimumPathSum)|Medium|
245246

246247
# Database
247248
|#|Title|Solution|Difficulty|

algorithms/MinimumPathSum/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Minimum Path Sum
2+
We can solve this problem by Dynamic Programming
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int minPathSum(int[][] grid) {
3+
int rowLength = grid.length,
4+
colLength = grid[0].length;
5+
6+
int[][] dp = new int[rowLength][colLength];
7+
8+
for (int i = 0; i < rowLength; i++) {
9+
for (int j = 0; j < colLength; j++) {
10+
if (i == 0 && j == 0) dp[i][j] = grid[i][j];
11+
else if (i == 0 && j > 0) dp[i][j] = grid[i][j] + dp[i][j - 1];
12+
else if (i > 0 && j == 0) dp[i][j] = grid[i][j] + dp[i - 1][j];
13+
else dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
14+
}
15+
}
16+
17+
return dp[rowLength - 1][colLength - 1];
18+
}
19+
}

algorithms/MinimumPathSum/solution.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
var minPathSum = function(grid) {
6+
let rowCount = grid.length,
7+
colCount = grid[0].length
8+
let dp = []
9+
10+
for (let i = 0; i < rowCount; i++) {
11+
dp.push(new Array(colCount))
12+
for (let j = 0; j < colCount; j++) {
13+
if (i === 0 && j === 0) dp[i][j] = grid[i][j]
14+
else if (i === 0 && j > 0) dp[i][j] = grid[i][j] + dp[i][j - 1]
15+
else if (i > 0 && j === 0) dp[i][j] = grid[i][j] + dp[i - 1][j]
16+
else dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1])
17+
}
18+
}
19+
20+
return dp[rowCount - 1][colCount - 1]
21+
};

algorithms/MinimumPathSum/solution.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def minPathSum(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
row_count = len(grid)
8+
col_count = len(grid[0])
9+
10+
dp = [[0] * col_count] * row_count
11+
12+
for i in xrange(row_count):
13+
for j in xrange(col_count):
14+
if i > 0 and j > 0:
15+
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1])
16+
elif i == 0 and j > 0:
17+
dp[i][j] = grid[i][j] + dp[i][j - 1]
18+
elif i > 0 and j == 0:
19+
dp[i][j] = grid[i][j] + dp[i - 1][j]
20+
else:
21+
dp[i][j] = grid[i][j]
22+
23+
return dp[row_count - 1][col_count - 1]

0 commit comments

Comments
 (0)