Skip to content

Commit 14f1718

Browse files
authored
Merge pull request #679 from LetMeFly666/3219
添加问题“3219.切蛋糕的最小总开销II”的代码和题解
2 parents 4176b54 + b1c31aa commit 14f1718

8 files changed

+355
-6
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-31 13:04:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-31 13:10:03
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
class Solution {
13+
public:
14+
ll minimumCost(int m, int n, vector<int>& horizontalCut, vector<int>& verticalCut) {
15+
sort(horizontalCut.begin(), horizontalCut.end(), greater<>());
16+
sort(verticalCut.begin(), verticalCut.end(), greater<>());
17+
int hCutTime = 0, vCutTime = 0;
18+
ll ans = 0;
19+
while (hCutTime < horizontalCut.size() || vCutTime < verticalCut.size()) {
20+
if (vCutTime == verticalCut.size() || hCutTime < horizontalCut.size() && horizontalCut[hCutTime] > verticalCut[vCutTime]) {
21+
ans += horizontalCut[hCutTime++] * (vCutTime + 1);
22+
} else {
23+
ans += verticalCut[vCutTime++] * (hCutTime + 1);
24+
}
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-31 13:23:11
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-31 13:37:03
6+
*/
7+
package main
8+
9+
import "slices"
10+
11+
func minimumCost(m int, n int, horizontalCut []int, verticalCut []int) (ans int64) {
12+
slices.SortFunc(horizontalCut, func(i, j int) int { return j - i; });
13+
slices.SortFunc(verticalCut, func(i, j int) int { return j - i; });
14+
var hCutTime, vCutTime int
15+
m, n = m - 1, n - 1
16+
for hCutTime < m || vCutTime < n {
17+
if vCutTime == n || hCutTime < m && horizontalCut[hCutTime] > verticalCut[vCutTime] {
18+
ans += int64(horizontalCut[hCutTime] * (vCutTime + 1))
19+
hCutTime++
20+
} else {
21+
ans += int64(verticalCut[vCutTime] * (hCutTime + 1))
22+
vCutTime++
23+
}
24+
}
25+
return
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-31 13:14:31
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-31 13:22:22
6+
*/
7+
import java.util.Arrays;
8+
9+
class Solution {
10+
public long minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {
11+
// Arrays.sort(horizontalCut, (a, b) -> b - a); // 不可,Arrays.sort(int[])时不支持自定义排序
12+
Arrays.sort(horizontalCut); // 那就从小到大排序然后下面反着遍历吧
13+
Arrays.sort(verticalCut);
14+
int hCutTime = m - 2, vCutTime = n - 2;
15+
long ans = 0;
16+
while (hCutTime >= 0 || vCutTime >= 0) {
17+
if (vCutTime < 0 || hCutTime >= 0 && horizontalCut[hCutTime] > verticalCut[vCutTime]) {
18+
ans += horizontalCut[hCutTime--] * (n - vCutTime - 1);
19+
} else {
20+
ans += verticalCut[vCutTime--] * (m - hCutTime - 1);
21+
}
22+
}
23+
return ans;
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-12-31 13:10:50
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-12-31 13:14:10
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def minimumCost(self, m: int, n: int, horizontalCut: List[int], verticalCut: List[int]) -> int:
11+
horizontalCut.sort(key=lambda a: -a)
12+
verticalCut.sort(key=lambda a: -a)
13+
ans = hCutTime = vCutTime = 0
14+
m, n = m - 1, n - 1
15+
while hCutTime < m or vCutTime < n:
16+
if vCutTime == n or hCutTime < m and horizontalCut[hCutTime] > verticalCut[vCutTime]:
17+
ans += horizontalCut[hCutTime] * (vCutTime + 1)
18+
hCutTime += 1
19+
else:
20+
ans += verticalCut[vCutTime] * (hCutTime + 1)
21+
vCutTime += 1
22+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@
736736
|3211.生成不含相邻零的二进制字符串|中等|<a href="https://leetcode.cn/problems/generate-binary-strings-without-adjacent-zeros/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/29/LeetCode%203211.%E7%94%9F%E6%88%90%E4%B8%8D%E5%90%AB%E7%9B%B8%E9%82%BB%E9%9B%B6%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143352841" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/generate-binary-strings-without-adjacent-zeros/solutions/2970587/letmefly-3211sheng-cheng-bu-han-xiang-li-sdh3/" target="_blank">LeetCode题解</a>|
737737
|3216.交换后字典序最小的字符串|简单|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-a-swap/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/30/LeetCode%203216.%E4%BA%A4%E6%8D%A2%E5%90%8E%E5%AD%97%E5%85%B8%E5%BA%8F%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143362223" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-a-swap/solutions/2971048/letmefly-3216jiao-huan-hou-zi-dian-xu-zu-nxp9/" target="_blank">LeetCode题解</a>|
738738
|3218.切蛋糕的最小总开销I|中等|<a href="https://leetcode.cn/problems/minimum-cost-for-cutting-cake-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/25/LeetCode%203218.%E5%88%87%E8%9B%8B%E7%B3%95%E7%9A%84%E6%9C%80%E5%B0%8F%E6%80%BB%E5%BC%80%E9%94%80I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144728332" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cost-for-cutting-cake-i/solutions/3030374/letmefly-3218qie-dan-gao-de-zui-xiao-zon-a1t6/" target="_blank">LeetCode题解</a>|
739+
|3219.切蛋糕的最小总开销II|困难|<a href="https://leetcode.cn/problems/minimum-cost-for-cutting-cake-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/31/LeetCode%203219.%E5%88%87%E8%9B%8B%E7%B3%95%E7%9A%84%E6%9C%80%E5%B0%8F%E6%80%BB%E5%BC%80%E9%94%80II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144849684" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cost-for-cutting-cake-ii/solutions/3035187/letmefly-3219qie-dan-gao-de-zui-xiao-zon-8y65/" target="_blank">LeetCode题解</a>|
739740
|3222.求出硬币游戏的赢家|简单|<a href="https://leetcode.cn/problems/find-the-winning-player-in-coin-game/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/05/LeetCode%203222.%E6%B1%82%E5%87%BA%E7%A1%AC%E5%B8%81%E6%B8%B8%E6%88%8F%E7%9A%84%E8%B5%A2%E5%AE%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143501415" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-winning-player-in-coin-game/solutions/2977629/letmefly-3222qiu-chu-ying-bi-you-xi-de-y-08j3/" target="_blank">LeetCode题解</a>|
740741
|3226.使两个整数相等的位更改次数|简单|<a href="https://leetcode.cn/problems/number-of-bit-changes-to-make-two-integers-equal/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/02/LeetCode%203226.%E4%BD%BF%E4%B8%A4%E4%B8%AA%E6%95%B4%E6%95%B0%E7%9B%B8%E7%AD%89%E7%9A%84%E4%BD%8D%E6%9B%B4%E6%94%B9%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143448117" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-bit-changes-to-make-two-integers-equal/solutions/2974558/letmefly-3226shi-liang-ge-zheng-shu-xian-j01h/" target="_blank">LeetCode题解</a>|
741742
|3238.求出胜利玩家的数目|简单|<a href="https://leetcode.cn/problems/find-the-number-of-winning-players/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/23/LeetCode%203238.%E6%B1%82%E5%87%BA%E8%83%9C%E5%88%A9%E7%8E%A9%E5%AE%B6%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143995245" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-number-of-winning-players/solutions/2998145/letmefly-3238qiu-chu-sheng-li-wan-jia-de-izbg/" target="_blank">LeetCode题解</a>|
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
---
2+
title: 3219.切蛋糕的最小总开销 II
3+
date: 2024-12-31 14:21:50
4+
tags: [题解, LeetCode, 困难, 贪心, 数组, 排序]
5+
---
6+
7+
# 【LetMeFly】3219.切蛋糕的最小总开销 II:贪心——先切贵的
8+
9+
力扣题目链接:[https://leetcode.cn/problems/minimum-cost-for-cutting-cake-ii/](https://leetcode.cn/problems/minimum-cost-for-cutting-cake-ii/)
10+
11+
<p>有一个&nbsp;<code>m x n</code>&nbsp;大小的矩形蛋糕,需要切成&nbsp;<code>1 x 1</code>&nbsp;的小块。</p>
12+
13+
<p>给你整数&nbsp;<code>m</code>&nbsp;,<code>n</code>&nbsp;和两个数组:</p>
14+
15+
<ul>
16+
<li><code>horizontalCut</code> 的大小为&nbsp;<code>m - 1</code>&nbsp;,其中&nbsp;<code>horizontalCut[i]</code>&nbsp;表示沿着水平线 <code>i</code>&nbsp;切蛋糕的开销。</li>
17+
<li><code>verticalCut</code> 的大小为&nbsp;<code>n - 1</code>&nbsp;,其中&nbsp;<code>verticalCut[j]</code>&nbsp;表示沿着垂直线&nbsp;<code>j</code>&nbsp;切蛋糕的开销。</li>
18+
</ul>
19+
20+
<p>一次操作中,你可以选择任意不是&nbsp;<code>1 x 1</code>&nbsp;大小的矩形蛋糕并执行以下操作之一:</p>
21+
22+
<ol>
23+
<li>沿着水平线&nbsp;<code>i</code>&nbsp;切开蛋糕,开销为&nbsp;<code>horizontalCut[i]</code>&nbsp;。</li>
24+
<li>沿着垂直线&nbsp;<code>j</code>&nbsp;切开蛋糕,开销为&nbsp;<code>verticalCut[j]</code>&nbsp;。</li>
25+
</ol>
26+
27+
<p>每次操作后,这块蛋糕都被切成两个独立的小蛋糕。</p>
28+
29+
<p>每次操作的开销都为最开始对应切割线的开销,并且不会改变。</p>
30+
31+
<p>请你返回将蛋糕全部切成&nbsp;<code>1 x 1</code>&nbsp;的蛋糕块的&nbsp;<strong>最小</strong>&nbsp;总开销。</p>
32+
33+
<p>&nbsp;</p>
34+
35+
<p><strong class="example">示例 1:</strong></p>
36+
37+
<div class="example-block">
38+
<p><span class="example-io"><b>输入:</b>m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]</span></p>
39+
40+
<p><span class="example-io"><b>输出:</b>13</span></p>
41+
42+
<p><strong>解释:</strong></p>
43+
44+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/04/ezgifcom-animated-gif-maker-1.gif" style="width: 280px; height: 320px;" /></p>
45+
46+
<ul>
47+
<li>沿着垂直线 0 切开蛋糕,开销为 5 。</li>
48+
<li>沿着水平线 0 切开&nbsp;<code>3 x 1</code>&nbsp;的蛋糕块,开销为 1 。</li>
49+
<li>沿着水平线 0 切开 <code>3 x 1</code>&nbsp;的蛋糕块,开销为 1 。</li>
50+
<li>沿着水平线 1 切开 <code>2 x 1</code>&nbsp;的蛋糕块,开销为 3 。</li>
51+
<li>沿着水平线 1 切开 <code>2 x 1</code>&nbsp;的蛋糕块,开销为 3 。</li>
52+
</ul>
53+
54+
<p>总开销为&nbsp;<code>5 + 1 + 1 + 3 + 3 = 13</code>&nbsp;。</p>
55+
</div>
56+
57+
<p><strong class="example">示例 2:</strong></p>
58+
59+
<div class="example-block">
60+
<p><span class="example-io"><b>输入:</b>m = 2, n = 2, horizontalCut = [7], verticalCut = [4]</span></p>
61+
62+
<p><span class="example-io"><b>输出:</b>15</span></p>
63+
64+
<p><strong>解释:</strong></p>
65+
66+
<ul>
67+
<li>沿着水平线 0 切开蛋糕,开销为 7 。</li>
68+
<li>沿着垂直线 0 切开&nbsp;<code>1 x 2</code>&nbsp;的蛋糕块,开销为 4 。</li>
69+
<li>沿着垂直线 0 切开&nbsp;<code>1 x 2</code>&nbsp;的蛋糕块,开销为 4 。</li>
70+
</ul>
71+
72+
<p>总开销为&nbsp;<code>7 + 4 + 4 = 15</code>&nbsp;。</p>
73+
</div>
74+
75+
<p>&nbsp;</p>
76+
77+
<p><strong>提示:</strong></p>
78+
79+
<ul>
80+
<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
81+
<li><code>horizontalCut.length == m - 1</code></li>
82+
<li><code>verticalCut.length == n - 1</code></li>
83+
<li><code>1 &lt;= horizontalCut[i], verticalCut[i] &lt;= 10<sup>3</sup></code></li>
84+
</ul>
85+
86+
87+
88+
## 解题方法:贪心(双指针)
89+
90+
从头到尾贯穿整个蛋糕的一刀切的越早,所需的切割次数越少。
91+
92+
> 例如假如一个2x2的蛋糕:
93+
>
94+
> 1. 先竖着切,就是竖着一刀横着两刀
95+
> 2. 先横着切,就是竖着两刀横着一刀
96+
97+
所以,将切割代价按照从大到小的顺序排序,然后在横竖切法里挑最贵的先切就好了。
98+
99+
切的时候从头切到尾:
100+
101+
> 假如这是竖着的一刀,就看横向一共切了几次。
102+
>
103+
> 如果横向一共切了$hCutTime$次,那么这一刀就要切$hCutTime + 1$次。
104+
105+
+ 时间复杂度$O(m\log m + n\log n)$
106+
+ 空间复杂度$O(\log m + \log n)$
107+
108+
### AC代码
109+
110+
#### C++
111+
112+
```cpp
113+
/*
114+
* @Author: LetMeFly
115+
* @Date: 2024-12-31 13:04:36
116+
* @LastEditors: LetMeFly.xyz
117+
* @LastEditTime: 2024-12-31 13:10:03
118+
*/
119+
#ifdef _WIN32
120+
#include "_[1,2]toVector.h"
121+
#endif
122+
123+
typedef long long ll;
124+
class Solution {
125+
public:
126+
ll minimumCost(int m, int n, vector<int>& horizontalCut, vector<int>& verticalCut) {
127+
sort(horizontalCut.begin(), horizontalCut.end(), greater<>());
128+
sort(verticalCut.begin(), verticalCut.end(), greater<>());
129+
int hCutTime = 0, vCutTime = 0;
130+
ll ans = 0;
131+
while (hCutTime < horizontalCut.size() || vCutTime < verticalCut.size()) {
132+
if (vCutTime == verticalCut.size() || hCutTime < horizontalCut.size() && horizontalCut[hCutTime] > verticalCut[vCutTime]) {
133+
ans += horizontalCut[hCutTime++] * (vCutTime + 1);
134+
} else {
135+
ans += verticalCut[vCutTime++] * (hCutTime + 1);
136+
}
137+
}
138+
return ans;
139+
}
140+
};
141+
```
142+
143+
#### Python
144+
145+
```python
146+
'''
147+
Author: LetMeFly
148+
Date: 2024-12-31 13:10:50
149+
LastEditors: LetMeFly.xyz
150+
LastEditTime: 2024-12-31 13:14:10
151+
'''
152+
from typing import List
153+
154+
class Solution:
155+
def minimumCost(self, m: int, n: int, horizontalCut: List[int], verticalCut: List[int]) -> int:
156+
horizontalCut.sort(key=lambda a: -a)
157+
verticalCut.sort(key=lambda a: -a)
158+
ans = hCutTime = vCutTime = 0
159+
m, n = m - 1, n - 1
160+
while hCutTime < m or vCutTime < n:
161+
if vCutTime == n or hCutTime < m and horizontalCut[hCutTime] > verticalCut[vCutTime]:
162+
ans += horizontalCut[hCutTime] * (vCutTime + 1)
163+
hCutTime += 1
164+
else:
165+
ans += verticalCut[vCutTime] * (hCutTime + 1)
166+
vCutTime += 1
167+
return ans
168+
```
169+
170+
#### Java
171+
172+
```java
173+
/*
174+
* @Author: LetMeFly
175+
* @Date: 2024-12-31 13:14:31
176+
* @LastEditors: LetMeFly.xyz
177+
* @LastEditTime: 2024-12-31 13:22:22
178+
*/
179+
import java.util.Arrays;
180+
181+
class Solution {
182+
public long minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {
183+
// Arrays.sort(horizontalCut, (a, b) -> b - a); // 不可,Arrays.sort(int[])时不支持自定义排序
184+
Arrays.sort(horizontalCut); // 那就从小到大排序然后下面反着遍历吧
185+
Arrays.sort(verticalCut);
186+
int hCutTime = m - 2, vCutTime = n - 2;
187+
long ans = 0;
188+
while (hCutTime >= 0 || vCutTime >= 0) {
189+
if (vCutTime < 0 || hCutTime >= 0 && horizontalCut[hCutTime] > verticalCut[vCutTime]) {
190+
ans += horizontalCut[hCutTime--] * (n - vCutTime - 1);
191+
} else {
192+
ans += verticalCut[vCutTime--] * (m - hCutTime - 1);
193+
}
194+
}
195+
return ans;
196+
}
197+
}
198+
```
199+
200+
#### Go
201+
202+
```go
203+
/*
204+
* @Author: LetMeFly
205+
* @Date: 2024-12-31 13:23:11
206+
* @LastEditors: LetMeFly.xyz
207+
* @LastEditTime: 2024-12-31 13:37:03
208+
*/
209+
package main
210+
211+
import "slices"
212+
213+
func minimumCost(m int, n int, horizontalCut []int, verticalCut []int) (ans int64) {
214+
slices.SortFunc(horizontalCut, func(i, j int) int { return j - i; });
215+
slices.SortFunc(verticalCut, func(i, j int) int { return j - i; });
216+
var hCutTime, vCutTime int
217+
m, n = m - 1, n - 1
218+
for hCutTime < m || vCutTime < n {
219+
if vCutTime == n || hCutTime < m && horizontalCut[hCutTime] > verticalCut[vCutTime] {
220+
ans += int64(horizontalCut[hCutTime] * (vCutTime + 1))
221+
hCutTime++
222+
} else {
223+
ans += int64(verticalCut[vCutTime] * (hCutTime + 1))
224+
vCutTime++
225+
}
226+
}
227+
return
228+
}
229+
```
230+
231+
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/12/31/LeetCode%203219.%E5%88%87%E8%9B%8B%E7%B3%95%E7%9A%84%E6%9C%80%E5%B0%8F%E6%80%BB%E5%BC%80%E9%94%80II/)哦~
232+
>
233+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/144849684](https://letmefly.blog.csdn.net/article/details/144849684)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ tags: [其他, 知识, 英语, Notes]
849849
|vowel|n. 元音,元音字母<br/>adj. 母音的<br/>v. v. 加母音(符号)于|
850850
|||
851851
|thereon|adv. 以...为依据,由...而产生,在其上|
852-
|reverence|n. 尊敬,尊重,尊崇<br/>v. 尊敬,尊重,尊崇|
852+
|<font color="#28bea0" title="二次复习">reverence</font>|n. 尊敬,尊重,尊崇<br/>v. 尊敬,尊重,尊崇|
853853
|||
854854
|hitchhike|v. 搭便车(旅行)|
855855
|memoir|n. 自传,(尤指名人的)回忆录,传说,地方志|
@@ -913,6 +913,8 @@ tags: [其他, 知识, 英语, Notes]
913913
|||
914914
|indignation|n. 慷慨,愤怒,义愤,气愤|
915915
|deform|v. 改变...的外形,损毁...的形状,使成畸形|
916+
|||
917+
|denominate|v. 以...(某种货币)为单位,将...命名为,称...为|
916918

917919
<p class="wordCounts">单词收录总数</p>
918920

0 commit comments

Comments
 (0)