Skip to content

Commit a2afeb7

Browse files
authored
Merge pull request #30 from LetMeFly666/2530
2530
2 parents 8ab5e74 + 8afc868 commit a2afeb7

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2023-10-18 09:12:11
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2023-10-18 09:38:39
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 maxKelements(vector<int>& nums, int k) {
15+
priority_queue<int> pq(nums.begin(), nums.end());
16+
ll ans = 0;
17+
while (k--) {
18+
int thisNum = pq.top();
19+
pq.pop();
20+
ans += thisNum;
21+
pq.push((thisNum + 2) / 3);
22+
}
23+
return ans;
24+
}
25+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2023-10-18 09:17:15
4+
LastEditors: LetMeFly
5+
LastEditTime: 2023-10-18 09:27:01
6+
'''
7+
from typing import List
8+
import heapq
9+
10+
class Solution:
11+
def maxKelements(self, nums: List[int], k: int) -> int:
12+
nums = list(map(lambda x: -x, nums))
13+
heapq.heapify(nums)
14+
ans = 0
15+
for _ in range(k):
16+
thisNum = -heapq.heappop(nums)
17+
ans += thisNum
18+
heapq.heappush(nums, -((thisNum + 2) // 3))
19+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ int main() {
508508
|2500.删除每行中的最大值|简单|<a href="https://leetcode.cn/problems/delete-greatest-value-in-each-row/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/07/27/LeetCode%202500.%E5%88%A0%E9%99%A4%E6%AF%8F%E8%A1%8C%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131951838" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/delete-greatest-value-in-each-row/solutions/2360649/letmefly-2500shan-chu-mei-xing-zhong-de-8ks5c/" target="_blank">LeetCode题解</a>|
509509
|2511.最多可以摧毁的敌人城堡数目|简单|<a href="https://leetcode.cn/problems/maximum-enemy-forts-that-can-be-captured/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/09/02/LeetCode%202511.%E6%9C%80%E5%A4%9A%E5%8F%AF%E4%BB%A5%E6%91%A7%E6%AF%81%E7%9A%84%E6%95%8C%E4%BA%BA%E5%9F%8E%E5%A0%A1%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132634912" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-enemy-forts-that-can-be-captured/solutions/2422392/letmefly-2511zui-duo-ke-yi-cui-hui-de-di-r7kt/" target="_blank">LeetCode题解</a>|
510510
|2512.奖励最顶尖的K名学生|中等|<a href="https://leetcode.cn/problems/reward-top-k-students/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/11/LeetCode%202512.%E5%A5%96%E5%8A%B1%E6%9C%80%E9%A1%B6%E5%B0%96%E7%9A%84K%E5%90%8D%E5%AD%A6%E7%94%9F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133762019" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reward-top-k-students/solutions/2477204/letmefly-2512jiang-li-zui-ding-jian-de-k-o0fg/" target="_blank">LeetCode题解</a>|
511+
|2530.执行K次操作后的最大分数|中等|<a href="https://leetcode.cn/problems/maximal-score-after-applying-k-operations/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/18/LeetCode%202530.%E6%89%A7%E8%A1%8CK%E6%AC%A1%E6%93%8D%E4%BD%9C%E5%90%8E%E7%9A%84%E6%9C%80%E5%A4%A7%E5%88%86%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133899145" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximal-score-after-applying-k-operations/solutions/2487511/letmefly-2530zhi-xing-k-ci-cao-zuo-hou-d-s4pm/" target="_blank">LeetCode题解</a>|
511512
|2544.交替数字和|简单|<a href="https://leetcode.cn/problems/alternating-digit-sum/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/07/12/LeetCode%202544.%E4%BA%A4%E6%9B%BF%E6%95%B0%E5%AD%97%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131673485" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/alternating-digit-sum/solutions/2340450/letmefly-2544jiao-ti-shu-zi-he-by-tisfy-aa0f/" target="_blank">LeetCode题解</a>|
512513
|2559.统计范围内的元音字符串数|中等|<a href="https://leetcode.cn/problems/count-vowel-strings-in-ranges/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/06/02/LeetCode%202559.%E7%BB%9F%E8%AE%A1%E8%8C%83%E5%9B%B4%E5%86%85%E7%9A%84%E5%85%83%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131014779" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-vowel-strings-in-ranges/solutions/2294361/letmefly-2559tong-ji-fan-wei-nei-de-yuan-mq4f/" target="_blank">LeetCode题解</a>|
513514
|2562.找出数组的串联值|简单|<a href="https://leetcode.cn/problems/find-the-array-concatenation-value/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/12/LeetCode%202562.%E6%89%BE%E5%87%BA%E6%95%B0%E7%BB%84%E7%9A%84%E4%B8%B2%E8%81%94%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133797249" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-array-concatenation-value/solutions/2479676/letmefly-2562zhao-chu-shu-zu-de-chuan-li-5atk/" target="_blank">LeetCode题解</a>|
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: 2530.执行 K 次操作后的最大分数
3+
date: 2023-10-18 09:32:49
4+
tags: [题解, LeetCode, 中等, 贪心, 数组, 堆(优先队列), 堆, 优先队列]
5+
---
6+
7+
# 【LetMeFly】2530.执行 K 次操作后的最大分数:优先队列(贪心)
8+
9+
力扣题目链接:[https://leetcode.cn/problems/maximal-score-after-applying-k-operations/](https://leetcode.cn/problems/maximal-score-after-applying-k-operations/)
10+
11+
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>k</code> 。你的 <strong>起始分数</strong> 为 <code>0</code> 。</p>
12+
13+
<p>在一步 <strong>操作</strong> 中:</p>
14+
15+
<ol>
16+
<li>选出一个满足 <code>0 &lt;= i &lt; nums.length</code> 的下标 <code>i</code> ,</li>
17+
<li>将你的 <strong>分数</strong> 增加 <code>nums[i]</code> ,并且</li>
18+
<li>将 <code>nums[i]</code> 替换为 <code>ceil(nums[i] / 3)</code> 。</li>
19+
</ol>
20+
21+
<p>返回在 <strong>恰好</strong> 执行 <code>k</code> 次操作后,你可能获得的最大分数。</p>
22+
23+
<p>向上取整函数 <code>ceil(val)</code> 的结果是大于或等于 <code>val</code> 的最小整数。</p>
24+
25+
<p>&nbsp;</p>
26+
27+
<p><strong>示例 1:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>nums = [10,10,10,10,10], k = 5
31+
<strong>输出:</strong>50
32+
<strong>解释:</strong>对数组中每个元素执行一次操作。最后分数是 10 + 10 + 10 + 10 + 10 = 50 。
33+
</pre>
34+
35+
<p><strong>示例 2:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>nums = [1,10,3,3,3], k = 3
39+
<strong>输出:</strong>17
40+
<strong>解释:</strong>可以执行下述操作:
41+
第 1 步操作:选中 i = 1 ,nums 变为 [1,<em><strong>4</strong></em>,3,3,3] 。分数增加 10 。
42+
第 2 步操作:选中 i = 1 ,nums 变为 [1,<em><strong>2</strong></em>,3,3,3] 。分数增加 4 。
43+
第 3 步操作:选中 i = 2 ,nums 变为 [1,1,<em><strong>1</strong></em>,3,3] 。分数增加 3 。
44+
最后分数是 10 + 4 + 3 = 17 。
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= nums.length, k &lt;= 10<sup>5</sup></code></li>
53+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
54+
</ul>
55+
56+
57+
58+
## 方法一:优先队列(贪心)
59+
60+
每次取一个数并累加到总分中,要想使总分最大,当然要选尽可能大的数。
61+
62+
因此使用一个大根堆,将数组中所有的整数加入堆栈(优先队列),并进行$k$次以下操作:
63+
64+
> 每次从队首(堆顶)取出一个元素累加,并将其三分之一(向上取整)重新入队。
65+
66+
最终返回累加的答案即可。
67+
68+
+ 时间复杂度$O(len(nums) + k\times \log len(nums))$
69+
+ 空间复杂度$O(len(nums))$
70+
71+
### AC代码
72+
73+
#### C++
74+
75+
```cpp
76+
typedef long long ll;
77+
class Solution {
78+
public:
79+
ll maxKelements(vector<int>& nums, int k) {
80+
priority_queue<int> pq;
81+
for (int t : nums) {
82+
pq.push(t);
83+
}
84+
ll ans = 0;
85+
while (k--) {
86+
int thisNum = pq.top();
87+
pq.pop();
88+
ans += thisNum;
89+
pq.push((thisNum + 2) / 3);
90+
}
91+
return ans;
92+
}
93+
};
94+
```
95+
96+
#### Python
97+
98+
```python
99+
# from typing import List
100+
# import heapq
101+
102+
class Solution:
103+
def maxKelements(self, nums: List[int], k: int) -> int:
104+
nums = list(map(lambda x: -x, nums))
105+
heapq.heapify(nums)
106+
ans = 0
107+
for _ in range(k):
108+
thisNum = -heapq.heappop(nums)
109+
ans += thisNum
110+
heapq.heappush(nums, -((thisNum + 2) // 3))
111+
return ans
112+
```
113+
114+
> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2023/10/18/LeetCode%202530.%E6%89%A7%E8%A1%8CK%E6%AC%A1%E6%93%8D%E4%BD%9C%E5%90%8E%E7%9A%84%E6%9C%80%E5%A4%A7%E5%88%86%E6%95%B0/)哦~
115+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/133899145](https://letmefly.blog.csdn.net/article/details/133899145)

0 commit comments

Comments
 (0)