当前位置:网站首页>Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)
Leetcode 1962. Remove stones to minimize the total amount (should be rounded up)
2022-06-11 14:04:00 【I'm not xiaohaiwa~~~~】

Give you an array of integers piles , Array Subscript from 0 Start , among piles[i] It means the first one i The number of stones in the pile . I'll give you another integer k , Please do the following just k Time :
Choose any pile of stones piles[i] , And from it remove floor(piles[i] / 2) Stone .
Be careful : You can be right The same pile The stone performs this operation multiple times .
Return execution k After the first operation , The remaining stones Minimum total .
floor(x) by Less than or be equal to x Of Maximum Integers .( namely , Yes x Rounding down ).( According to the example , The error is described here )
Example 1:
Input :piles = [5,4,9], k = 2
Output :12
explain : Possible execution scenarios are as follows :
- Right. 2 Remove the rockfill , The distribution of stones becomes [5,4,5] .
- Right. 0 Remove the rockfill , The distribution of stones becomes [3,4,5] .
The total number of stones left is 12 .
Example 2:
Input :piles = [4,3,6,7], k = 3
Output :12
explain : Possible execution scenarios are as follows :
- Right. 2 Remove the rockfill , The distribution of stones becomes [4,3,3,7] .
- Right. 3 Remove the rockfill , The distribution of stones becomes [4,3,3,4] .
- Right. 0 Remove the rockfill , The distribution of stones becomes [2,3,3,4] .
The total number of stones left is 12 .
Tips :
- 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;
}
};
边栏推荐
- Pki/tls Swiss Army knife cfssl
- SAP Spartacus checkout 流程使用 url 粘贴直接跳转到 delivery mode不能打开页面的原因
- 强大的全文本搜索工具——AnyTXT Searcher
- How to manage the server to make the website stable and smooth
- mysql高级语句
- AGV机器人RFID传感器CK-G06A与西门子1200PLC应用手册
- Using vscode code code template to improve mobx coding efficiency
- 折叠表达式
- YOLOv3学习笔记:YOLOv3的模型结构
- 2022 Gansu Province safety officer B certificate test question simulation test question bank and online simulation test
猜你喜欢

Collapse expression

强大的全文本搜索工具——AnyTXT Searcher

Tp6 whoops based exception takeover (beautiful interface)

Ecplise cannot connect to SQL Server

Redis configuration and optimization of NoSQL

cadence SPB17.4 - group operation(add to group, view group list, delete group)

YOLOv3学习笔记:YOLOv3的模型结构

全球手机市场衰退,连苹果也对iPhone14不抱过高期待
Explanation of waitgroup usage in go language learning

Three level classification display
随机推荐
C language learning record 6
Using vscode code code template to improve mobx coding efficiency
[the path of system analyst] collection of wrong topics of system analyst
Stochastic dynamic economic dispatching model of wind power (realized by matlab code)
【Flink】Flink CancellationException null DefaultExecutionGraphCache LeaderRetrievalHandler
强大的全文本搜索工具——AnyTXT Searcher
Collapse expression
Introduction to reverse learning - excellent assembly debugging tool OllyDbg
可变参表达式
Variable parameter expression
Which exchange is PTA futures on? How can PTA futures be safe?
My struggle: my years in foreign enterprises (1)
How to manage the server to make the website stable and smooth
使用cpolar远程办公(1)
2022工具钳工(中级)操作证考试题库及答案
Introduction to long connection
Hamad application scheduling scheme 06 of hashicopy (configure task)
MySQL数据库创建索引的方法和好处
CVPR 2022 | neural radiation field geometry editing method nerf editing
VIM secondary replacement