File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 64. Minimum Path Sum
3
+ * https://leetcode.com/problems/minimum-path-sum/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given a m x n grid filled with non-negative numbers, find a path from
7
+ * top left to bottom right, which minimizes the sum of all numbers along
8
+ * its path.
9
+ *
10
+ * Note: You can only move either down or right at any point in time.
11
+ */
12
+
13
+ /**
14
+ * @param {number[][] } grid
15
+ * @return {number }
16
+ */
17
+ var minPathSum = function ( grid ) {
18
+ for ( let i = 0 ; i < grid . length ; i ++ ) {
19
+ for ( let j = 0 ; j < grid [ 0 ] . length ; j ++ ) {
20
+ if ( i === 0 && j === 0 ) grid [ i ] [ j ] = grid [ i ] [ j ] ;
21
+ else if ( i === 0 && j !== 0 ) grid [ i ] [ j ] = grid [ i ] [ j ] + grid [ i ] [ j - 1 ] ;
22
+ else if ( i !== 0 && j === 0 ) grid [ i ] [ j ] = grid [ i ] [ j ] + grid [ i - 1 ] [ j ] ;
23
+ else grid [ i ] [ j ] = grid [ i ] [ j ] + Math . min ( grid [ i - 1 ] [ j ] , grid [ i ] [ j - 1 ] ) ;
24
+ }
25
+ }
26
+
27
+ return grid [ grid . length - 1 ] [ grid [ 0 ] . length - 1 ] ;
28
+ } ;
Original file line number Diff line number Diff line change 64
64
58|[ Length of Last Word] ( ./0058-length-of-last-word.js ) |Easy|
65
65
59|[ Spiral Matrix II] ( ./0059-spiral-matrix-ii.js ) |Medium|
66
66
62|[ Unique Paths] ( ./0062-unique-paths.js ) |Medium|
67
+ 64|[ Minimum Path Sum] ( ./0064-minimum-path-sum.js ) |Medium|
67
68
66|[ Plus One] ( ./0066-plus-one.js ) |Easy|
68
69
67|[ Add Binary] ( ./0067-add-binary.js ) |Easy|
69
70
69|[ Sqrt(x)] ( ./0069-sqrtx.js ) |Medium|
You can’t perform that action at this time.
0 commit comments