当前位置:网站首页>Leetcode 1962. 移除石子使总数最小(应该是向上取整)
Leetcode 1962. 移除石子使总数最小(应该是向上取整)
2022-06-11 14:02:00 【我不是萧海哇~~~~】

给你一个整数数组 piles ,数组 下标从 0 开始 ,其中 piles[i] 表示第 i 堆石子中的石子数量。另给你一个整数 k ,请你执行下述操作 恰好 k 次:
选出任一石子堆 piles[i] ,并从中 移除 floor(piles[i] / 2) 颗石子。
注意:你可以对 同一堆 石子多次执行此操作。
返回执行 k 次操作后,剩下石子的 最小 总数。
floor(x) 为 小于 或 等于 x 的 最大 整数。(即,对 x 向下取整)。(根据例子,这里描述错误)
示例 1:
输入:piles = [5,4,9], k = 2
输出:12
解释:可能的执行情景如下:
- 对第 2 堆石子执行移除操作,石子分布情况变成 [5,4,5] 。
- 对第 0 堆石子执行移除操作,石子分布情况变成 [3,4,5] 。
剩下石子的总数为 12 。
示例 2:
输入:piles = [4,3,6,7], k = 3
输出:12
解释:可能的执行情景如下:
- 对第 2 堆石子执行移除操作,石子分布情况变成 [4,3,3,7] 。
- 对第 3 堆石子执行移除操作,石子分布情况变成 [4,3,3,4] 。
- 对第 0 堆石子执行移除操作,石子分布情况变成 [2,3,3,4] 。
剩下石子的总数为 12 。
提示:
- 1 <= piles.length <= 105
- 1 <= piles[i] <= 104
- 1 <= k <= 105
Code:
class Solution {
public:
int minStoneSum(vector<int>& piles, int k) {
priority_queue<int>q;
for(int i=0;i<piles.size();i++)
{
q.push(piles[i]);
}
for(int i=0;i<k;i++)
{
int num;
if(q.top()%2)
{
num=q.top()/2+1;
}
else
num=q.top()/2;
q.pop();
q.push(num);
}
int res=0;
while (q.size()) {
res+=q.top();
q.pop();
}
return res;
}
};
边栏推荐
- Using vscode code code template to improve mobx coding efficiency
- [201] PHP exception handling - try catch finally exception handling in PHP
- Redis uses 10 tips, please accept!
- Interview to build an aircraft carrier, screw on the job, pay
- Invalid bound statement (not found) error [resolved]
- cadence SPB17.4 - allegro - allegro_free_viewer
- Powerful full text search tool anytxt searcher
- Installation and use of Anaconda
- Checkout design in SAP Spartacus
- 解决循环依赖BUG。Relying upon circular references is discouraged and they are prohibited by default.
猜你喜欢

AGV机器人RFID传感器CK-G06A与西门子1200PLC应用手册

Using vscode code code template to improve mobx coding efficiency

Musk says he doesn't like being a CEO, but rather wants to do technology and design; Wu Enda's "machine learning" course is about to close its registration | geek headlines

Code comparison tool, I use these six

Today in history: Apple II comes out; Microsoft acquires gecad; The scientific and technological pioneer who invented the word "software engineering" was born

Variable parameter expression

BS-XX-007基于JSP实现户籍管理系统

全球手机市场衰退,连苹果也对iPhone14不抱过高期待

Invalid bound statement (not found)错误【已解决】

HR doesn't want to read such a PDF technical resume at all. How can it be in the hands of the interviewer?
随机推荐
Question bank and answers for 2022 tool fitter (intermediate) operation certificate examination
C # set the cursor shape of forms and systems
面试造航母,入职拧螺丝,工资...
Hamad application scheduling scheme 06 of hashicopy (configure task)
LNMP deployment
论文导读 | 机器学习在数据库基数估计中的应用
Kubernetes binary installation (v1.20.15) (VI) deploying worknode nodes
VIM secondary replacement
Kubernetes binary installation (v1.20.15) (VII) plug a work node
Nomad application layout scheme 07 of hashicopy (submit job)
可变参表达式
Interview to build an aircraft carrier, screw on the job, pay
Kubernetes binary installation (v1.20.16) (V) verifying master deployment
Invalid bound statement (not found) error [resolved]
2022工具钳工(中级)操作证考试题库及答案
AGV robot RFID sensor ck-g06a and Siemens 1200plc Application Manual
Network information system emergency response
Introduction to reverse learning - excellent assembly debugging tool OllyDbg
d区间到可空转换
Container -- reverse content -- use of explosion, splicing, and inversion functions