Skip to content

Commit d40cce1

Browse files
authored
Create 3219-minimum-cost-for-cutting-cake-ii.js
1 parent 721f84e commit d40cce1

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Cut {
2+
constructor(cost, type) {
3+
this.cost = cost
4+
this.type = type
5+
}
6+
}
7+
/**
8+
* @param {number} m
9+
* @param {number} n
10+
* @param {number[]} horizontalCut
11+
* @param {number[]} verticalCut
12+
* @return {number}
13+
*/
14+
var minimumCost = function(m, n, horizontalCut, verticalCut) {
15+
let cuts = []
16+
for (let i = 0; i < horizontalCut.length; i++) {
17+
cuts.push(new Cut(horizontalCut[i], 'H'))
18+
}
19+
for (let j = 0; j < verticalCut.length; j++) {
20+
cuts.push(new Cut(verticalCut[j], 'V'))
21+
}
22+
23+
cuts.sort((a, b) => -a.cost + b.cost)
24+
25+
let ans = 0
26+
let hCount = 1
27+
let vCount = 1
28+
29+
for (let cut of cuts) {
30+
if (cut.type === 'H') {
31+
ans += cut.cost * vCount
32+
hCount++
33+
} else {
34+
ans += cut.cost * hCount
35+
vCount++
36+
}
37+
}
38+
39+
return ans
40+
};

0 commit comments

Comments
 (0)