-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjs.js
More file actions
33 lines (27 loc) · 719 Bytes
/
js.js
File metadata and controls
33 lines (27 loc) · 719 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
/**
* @param {number} row
* @param {number} col
* @param {number[][]} waitCost
* @return {number}
*/
function minCost(row, col, waitCost) {
// 2D array
const cache = Array.from({ length: row }, () => Array.from({ length: col }, () => Infinity))
function dp(i, j) {
if (i < 0 || j < 0) {
return Infinity
}
if (i === 0 && j === 0) {
return 1
}
if (cache[i][j] !== Infinity) {
return cache[i][j]
}
const entryCost = (i + 1) * (j + 1)
const nextWaitCost = waitCost[i][j]
const cost = Math.min(dp(i - 1, j), dp(i, j - 1)) + entryCost + nextWaitCost
cache[i][j] = cost
return cost
}
return dp(row - 1, col - 1) - waitCost[row - 1][col - 1]
}