题目链接: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 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
|