Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 328d742

Browse files
Add solution for Minimum Path Sum
1 parent cc2bb1e commit 328d742

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Algorithm exercises from LeetCode implemented in Java v11.
131131
### Dynamic Programming
132132
- Count Sorted Vowel Strings | [Problem](https://leetcode.com/problems/count-sorted-vowel-strings) | [Solution](src/solutions/CountSortedVowelStrings.java)
133133
- Count Square Submatrices with All Ones | [Problem](https://leetcode.com/problems/count-square-submatrices-with-all-ones) | [Solution](src/solutions/CountSquareSubmatrices.java)
134+
- Minimum Path Sum | [Problem](https://leetcode.com/problems/minimum-path-sum) | [Solution](src/solutions/MinimumPathSum.java)
134135
- Minimum Falling Path Sum | [Problem](https://leetcode.com/problems/minimum-falling-path-sum) | [Solution](src/solutions/MinimumFallingPathSum.java)
135136
- Maximum Number of Points with Cost | [Problem](https://leetcode.com/problems/maximum-number-of-points-with-cost) | [Solution](src/solutions/MaximumNumberOfPointsWithCost.java)
136137
- Partition Equal Subset Sum | [Problem](https://leetcode.com/problems/partition-equal-subset-sum) | [Solution](src/solutions/PartitionEqualSubsetSum.java)

‎src/solutions/MinimumPathSum.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package solutions;
2+
3+
// [Problem] https://leetcode.com/problems/minimum-path-sum
4+
class MinimumPathSum {
5+
// Bottom-up DP
6+
// O(m * n) time, O(m * n) space
7+
public int minPathSum(int[][] grid) {
8+
int rowSize = grid.length, colSize = grid[0].length;
9+
int[][] dp = new int[rowSize][colSize];
10+
for (int row = 0; row < rowSize; row++) {
11+
for (int col = 0; col < colSize; col++) {
12+
dp[row][col] = grid[row][col];
13+
if (row == 0 && col > 0) {
14+
dp[row][col] += dp[row][col - 1];
15+
} else if (row > 0 && col == 0) {
16+
dp[row][col] += dp[row - 1][col];
17+
} else if (row > 0 && col > 0) {
18+
dp[row][col] += Math.min(dp[row][col - 1], dp[row - 1][col]);
19+
}
20+
}
21+
}
22+
return dp[rowSize - 1][colSize - 1];
23+
}
24+
25+
// Test
26+
public static void main(String[] args) {
27+
MinimumPathSum solution = new MinimumPathSum();
28+
29+
int[][] input1 = {
30+
{1, 3, 1},
31+
{1, 5, 1},
32+
{4, 2, 1}
33+
};
34+
int expectedOutput1 = 7;
35+
int actualOutput1 = solution.minPathSum(input1);
36+
System.out.println("Test 1 passed? " + (expectedOutput1 == actualOutput1));
37+
38+
int[][] input2 = {
39+
{1, 2, 3},
40+
{4, 5, 6}
41+
};
42+
int expectedOutput2 = 12;
43+
int actualOutput2 = solution.minPathSum(input2);
44+
System.out.println("Test 2 passed? " + (expectedOutput2 == actualOutput2));
45+
}
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /