Skip to content

Commit 18786fc

Browse files
authored
Merge pull request #192 from LetMeFly666/2171
finished 2171
2 parents 037604b + 571fcbb commit 18786fc

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
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-01-18 19:20:51
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-01-18 19:32:10
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
long long minimumRemoval(vector<int>& beans) {
14+
sort(beans.begin(), beans.end());
15+
long long all = accumulate(beans.begin(), beans.end(), 0LL);
16+
long long ans = all;
17+
for (int i = 0; i < beans.size(); i++) {
18+
ans = min(ans, all - ((long long)beans.size() - i) * beans[i]);
19+
}
20+
return ans;
21+
}
22+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-01-18 19:32:37
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-01-18 19:35:01
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def minimumRemoval(self, beans: List[int]) -> int:
11+
beans.sort()
12+
all_ = sum(beans)
13+
ans = all_
14+
for i, thisBean in enumerate(beans):
15+
ans = min(ans, all_ - thisBean * (len(beans) - i))
16+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@
428428
|2105.给植物浇水II|中等|<a href="https://leetcode.cn/problems/watering-plants-ii/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2022/09/05/LeetCode%202105.%E7%BB%99%E6%A4%8D%E7%89%A9%E6%B5%87%E6%B0%B4II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126709258" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/watering-plants-ii/solution/by-tisfy-0erl/" target="_blank">LeetCode题解</a>|
429429
|2106.摘水果|困难|<a href="https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/05/04/LeetCode%202106.%E6%91%98%E6%B0%B4%E6%9E%9C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130482457" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/solutions/2255124/letmefly-2106zhai-shui-guo-by-tisfy-tb8v/" target="_blank">LeetCode题解</a>|
430430
|2132.用邮票贴满网格图|困难|<a href="https://leetcode.cn/problems/stamping-the-grid/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/12/14/LeetCode%202132.%E7%94%A8%E9%82%AE%E7%A5%A8%E8%B4%B4%E6%BB%A1%E7%BD%91%E6%A0%BC%E5%9B%BE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135002925" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/stamping-the-grid/solutions/2566644/letmefly-2132yong-you-piao-tie-man-wang-znigh/" target="_blank">LeetCode题解</a>|
431+
|2171.拿出最少数目的魔法豆|中等|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/18/LeetCode%202171.%E6%8B%BF%E5%87%BA%E6%9C%80%E5%B0%91%E6%95%B0%E7%9B%AE%E7%9A%84%E9%AD%94%E6%B3%95%E8%B1%86/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135682601" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/solutions/2609831/letmefly-2171na-chu-zui-shao-shu-mu-de-m-jima/" target="_blank">LeetCode题解</a>|
431432
|2178.拆分成最多数目的正偶数之和|中等|<a href="https://leetcode.cn/problems/maximum-split-of-positive-even-integers/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/07/06/LeetCode%202178.%E6%8B%86%E5%88%86%E6%88%90%E6%9C%80%E5%A4%9A%E6%95%B0%E7%9B%AE%E7%9A%84%E6%AD%A3%E5%81%B6%E6%95%B0%E4%B9%8B%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131567568" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-split-of-positive-even-integers/solutions/2332513/letmefly-2178chai-fen-cheng-zui-duo-shu-m44er/" target="_blank">LeetCode题解</a>|
432433
|2180.统计各位数字之和为偶数的整数个数|简单|<a href="https://leetcode.cn/problems/count-integers-with-even-digit-sum/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/01/06/LeetCode%202180.%E7%BB%9F%E8%AE%A1%E5%90%84%E4%BD%8D%E6%95%B0%E5%AD%97%E4%B9%8B%E5%92%8C%E4%B8%BA%E5%81%B6%E6%95%B0%E7%9A%84%E6%95%B4%E6%95%B0%E4%B8%AA%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128583772" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-integers-with-even-digit-sum/solutions/2048274/letmefly-2180tong-ji-ge-wei-shu-zi-zhi-h-g2ib/" target="_blank">LeetCode题解</a>|
433434
|2208.将数组和减半的最少操作次数|中等|<a href="https://leetcode.cn/problems/minimum-operations-to-halve-array-sum/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/07/25/LeetCode%202208.%E5%B0%86%E6%95%B0%E7%BB%84%E5%92%8C%E5%87%8F%E5%8D%8A%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131908685" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-halve-array-sum/solutions/2357632/letmefly-2208jiang-shu-zu-he-jian-ban-de-5gl0/" target="_blank">LeetCode题解</a>|
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: 2171.拿出最少数目的魔法豆
3+
date: 2024-01-18 19:29:06
4+
tags: [题解, LeetCode, 中等, 数组, 前缀和, 排序]
5+
---
6+
7+
# 【LetMeFly】2171.拿出最少数目的魔法豆:排序 + 枚举
8+
9+
力扣题目链接:[https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/](https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/)
10+
11+
<p>给你一个 <strong>正</strong>&nbsp;整数数组&nbsp;<code>beans</code>&nbsp;,其中每个整数表示一个袋子里装的魔法豆的数目。</p>
12+
13+
<p>请你从每个袋子中&nbsp;<strong>拿出</strong>&nbsp;一些豆子(也可以<strong>&nbsp;不拿出</strong>),使得剩下的 <strong>非空</strong> 袋子中(即 <strong>至少</strong>&nbsp;还有 <strong>一颗</strong>&nbsp;魔法豆的袋子)魔法豆的数目&nbsp;<strong>相等</strong>&nbsp;。一旦魔法豆从袋子中取出,你不能将它放到任何其他的袋子中。</p>
14+
15+
<p>请你返回你需要拿出魔法豆的 <strong>最少数目</strong>。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre><b>输入:</b>beans = [4,<em><strong>1</strong></em>,6,5]
22+
<b>输出:</b>4
23+
<b>解释:</b>
24+
- 我们从有 1 个魔法豆的袋子中拿出 1 颗魔法豆。
25+
剩下袋子中魔法豆的数目为:[4,<em><b>0</b></em>,6,5]
26+
- 然后我们从有 6 个魔法豆的袋子中拿出 2 个魔法豆。
27+
剩下袋子中魔法豆的数目为:[4,0,<em><strong>4</strong></em>,5]
28+
- 然后我们从有 5 个魔法豆的袋子中拿出 1 个魔法豆。
29+
剩下袋子中魔法豆的数目为:[4,0,4,<em><b>4</b></em>]
30+
总共拿出了 1 + 2 + 1 = 4 个魔法豆,剩下非空袋子中魔法豆的数目相等。
31+
没有比取出 4 个魔法豆更少的方案。
32+
</pre>
33+
34+
<p><strong>示例 2:</strong></p>
35+
36+
<pre><b>输入:</b>beans = [<em><strong>2</strong></em>,10,<em><strong>3</strong></em>,<em><strong>2</strong></em>]
37+
<b>输出:</b>7
38+
<strong>解释:</strong>
39+
- 我们从有 2 个魔法豆的其中一个袋子中拿出 2 个魔法豆。
40+
剩下袋子中魔法豆的数目为:[<em><strong>0</strong></em>,10,3,2]
41+
- 然后我们从另一个有 2 个魔法豆的袋子中拿出 2 个魔法豆。
42+
剩下袋子中魔法豆的数目为:[0,10,3,<em><strong>0</strong></em>]
43+
- 然后我们从有 3 个魔法豆的袋子中拿出 3 个魔法豆。
44+
剩下袋子中魔法豆的数目为:[0,10,<em><strong>0</strong></em>,0]
45+
总共拿出了 2 + 2 + 3 = 7 个魔法豆,剩下非空袋子中魔法豆的数目相等。
46+
没有比取出 7 个魔法豆更少的方案。
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= beans.length &lt;= 10<sup>5</sup></code></li>
55+
<li><code>1 &lt;= beans[i] &lt;= 10<sup>5</sup></code></li>
56+
</ul>
57+
58+
59+
60+
## 方法一:排序 + 枚举
61+
62+
二话不说先将豆子数量从小到大排个序。
63+
64+
统计一下一共有多少个豆子(即为all)。
65+
66+
遍历每个袋子中豆子的数量(假设当前豆子数量为bean),计算将小于bean的袋子移空、大于等于bean的袋子移为bean所需移除的豆子总数。
67+
68+
返回所有总数中的最小值即为答案。
69+
70+
+ 时间复杂度$O(len(beans))$
71+
+ 空间复杂度$O(len(beans))$
72+
73+
### AC代码
74+
75+
#### C++
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
long long minimumRemoval(vector<int>& beans) {
81+
sort(beans.begin(), beans.end());
82+
long long all = accumulate(beans.begin(), beans.end(), 0LL);
83+
long long ans = all;
84+
for (int i = 0; i < beans.size(); i++) {
85+
ans = min(ans, all - ((long long)beans.size() - i) * beans[i]);
86+
}
87+
return ans;
88+
}
89+
};
90+
```
91+
92+
#### Python
93+
94+
```python
95+
# from typing import List
96+
97+
class Solution:
98+
def minimumRemoval(self, beans: List[int]) -> int:
99+
beans.sort()
100+
all_ = sum(beans)
101+
ans = all_
102+
for i, thisBean in enumerate(beans):
103+
ans = min(ans, all_ - thisBean * (len(beans) - i))
104+
return ans
105+
```
106+
107+
> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2024/01/18/LeetCode%202171.%E6%8B%BF%E5%87%BA%E6%9C%80%E5%B0%91%E6%95%B0%E7%9B%AE%E7%9A%84%E9%AD%94%E6%B3%95%E8%B1%86/)哦~
108+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/135682601](https://letmefly.blog.csdn.net/article/details/135682601)

0 commit comments

Comments
 (0)