File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed
Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change 11# LeetCode Algorithms
22
3- ![ problems-solved] ( https://img.shields.io/badge/Problems%20Solved-442 /2081-1f425f.svg )
4- ![ problems-solved-java] ( https://img.shields.io/badge/Java-442 /2081-1abc9c.svg )
3+ ![ problems-solved] ( https://img.shields.io/badge/Problems%20Solved-460 /2081-1f425f.svg )
4+ ![ problems-solved-java] ( https://img.shields.io/badge/Java-460 /2081-1abc9c.svg )
55![ problems-solved-python] ( https://img.shields.io/badge/Python-186/2081-1abc9c.svg )
66[ ![ PRs Welcome] ( https://img.shields.io/badge/PRs-welcome-brightgreen.svg )] ( CONTRIBUTING.md )
77[ ![ cp] ( https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg )] ( https://github.com/anishLearnsToCode/competitive-programming )
6161| 61 | [ Rotate List] ( https://leetcode.com/problems/rotate-list ) | [ ![ Java] ( assets/java.png )] ( src/RotateList.java ) | |
6262| 62 | [ Unique Paths] ( https://leetcode.com/problems/unique-paths ) | [ ![ Java] ( assets/java.png )] ( src/UniquePaths.java ) | |
6363| 63 | [ Unique Paths II] ( https://leetcode.com/problems/unique-paths-ii ) | [ ![ Java] ( assets/java.png )] ( src/UniquePathII.java ) | |
64+ | 64 | [ Minimum Path Sum] ( https://leetcode.com/problems/minimum-path-sum ) | [ ![ Java] ( assets/java.png )] ( src/MinimumPathSum.java ) | |
6465| 66 | [ Plus One] ( https://leetcode.com/problems/plus-one ) | [ ![ Java] ( assets/java.png )] ( src/PlusOne.java ) [ ![ Python] ( assets/python.png )] ( python/plus_one.py ) | |
6566| 67 | [ Add Binary] ( https://leetcode.com/problems/add-binary ) | [ ![ Java] ( assets/java.png )] ( src/AddBinary.java ) [ ![ Python] ( assets/python.png )] ( python/add_binary.py ) | |
6667| 69 | [ Sqrt(x)] ( https://leetcode.com/problems/sqrtx ) | [ ![ Java] ( assets/java.png )] ( src/Sqrtx.java ) [ ![ Python] ( assets/python.png )] ( python/sqrt.py ) | |
Original file line number Diff line number Diff line change 1+ // https://leetcode.com/problems/minimum-path-sum
2+ // T: O(m * n)
3+ // S: O(1)
4+
5+ public class MinimumPathSum {
6+ public int minPathSum (int [][] grid ) {
7+ final int rows = grid .length , columns = grid [0 ].length ;
8+
9+ // last row
10+ for (int column = columns - 2 ; column >= 0 ; column --) {
11+ grid [rows - 1 ][column ] += grid [rows - 1 ][column + 1 ];
12+ }
13+
14+ // last column
15+ for (int row = rows - 2 ; row >= 0 ; row --) {
16+ grid [row ][columns - 1 ] += grid [row + 1 ][columns - 1 ];
17+ }
18+
19+ for (int row = rows - 2 ; row >= 0 ; row --) {
20+ for (int column = columns - 2 ; column >= 0 ; column --) {
21+ grid [row ][column ] += Math .min (grid [row + 1 ][column ], grid [row ][column + 1 ]);
22+ }
23+ }
24+
25+ return grid [0 ][0 ];
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments