-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminPathSum.js
More file actions
44 lines (35 loc) · 811 Bytes
/
Copy pathminPathSum.js
File metadata and controls
44 lines (35 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @param {number[][]} grid
* @return {number}
*/
const minPathSum = function (grid) {
const m = grid.length
const n = grid[0].length
const dp = []
const queue = [[0, 0]]
while (queue.length > 0) {
const [i, j] = queue.shift()
if (!dp[i]) {
dp[i] = []
}
if (i === 0 && j === 0) {
dp[i][j] = grid[i][j]
} else {
const up = (dp[i - 1] === undefined)
? Number.MAX_SAFE_INTEGER
: dp[i - 1][j]
const left = (dp[i][j - 1] === undefined)
? Number.MAX_SAFE_INTEGER
: dp[i][j - 1]
dp[i][j] = Math.min(up, left) + grid[i][j]
}
if (j < n - 1) {
queue.push([i, j + 1])
}
if (j === 0 && i < m - 1) {
queue.push([i + 1, j])
}
}
return dp[m - 1][n - 1]
}
module.exports = minPathSum