lc3218. 切蛋糕的最小总开销 i

题目链接:3218. 切蛋糕的最小总开销 I

题解

LC3219. 切蛋糕的最小总开销 II 的简化版本

参考代码

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
class Solution:
def minimumCost(self, m: int, n: int, horizontalCut: List[int], verticalCut: List[int]) -> int:

horizontalCut.sort()
verticalCut.sort()
cost = 0
i = 0
j = 0
while True:
if m == 1 and n == 1:
break
# print(i, j, m, n)
if n == 1:
cost += horizontalCut[i]
i += 1
m -= 1
elif m == 1:
cost += verticalCut[j]
j += 1
n -= 1
elif horizontalCut[i] < verticalCut[j]:
cost += horizontalCut[i] * n
i += 1
m -= 1
else:
cost += verticalCut[j] * m
j += 1
n -= 1

return cost

lc3218. 切蛋糕的最小总开销 i

https://blog.xiang578.com/problem/lc3218.html

作者

Ryen Xiang

发布于

2025-01-01

更新于

2025-01-01

许可协议


网络回响

评论