-
Notifications
You must be signed in to change notification settings - Fork 225
/
1029-two-city-scheduling.js
66 lines (62 loc) · 1.34 KB
/
1029-two-city-scheduling.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @param {number[][]} costs
* @return {number}
*/
const twoCitySchedCost = function(costs) {
const N = costs.length / 2
const dp = Array.from({ length: N + 1 }, () => new Array(N + 1).fill(0))
for (let i = 1; i <= N; i++) {
dp[i][0] = dp[i - 1][0] + costs[i - 1][0]
}
for (let j = 1; j <= N; j++) {
dp[0][j] = dp[0][j - 1] + costs[j - 1][1]
}
for (let i = 1; i <= N; i++) {
for (let j = 1; j <= N; j++) {
dp[i][j] = Math.min(
dp[i - 1][j] + costs[i + j - 1][0],
dp[i][j - 1] + costs[i + j - 1][1]
)
}
}
return dp[N][N]
}
// another
/**
* @param {number[][]} costs
* @return {number}
*/
const twoCitySchedCost = function(costs) {
const N = costs.length
let res = 0
const refund = []
for(let i = 0; i < N; i++) {
refund[i] = costs[i][1] - costs[i][0]
res += costs[i][0]
}
refund.sort((a, b) => a - b)
for(let i = 0; i < N / 2; i++) {
res += refund[i]
}
return res
};
// another
/**
* @param {number[][]} costs
* @return {number}
*/
const twoCitySchedCost = function(costs) {
const len = costs.length
if(len === 0) return 0
const N = len / 2
costs.sort((a, b) => (a[0] - a[1]) - (b[0] - b[1]))
let res = 0
for(let i = 0; i < costs.length; i++) {
if(i < N) {
res += costs[i][0]
} else {
res += costs[i][1]
}
}
return res
};