当前位置:网站首页>2279. Maximum number of backpacks filled with stones
2279. Maximum number of backpacks filled with stones
2022-07-02 15:40:00 【Laver (nori)】
class Solution {
public:
int maximumBags(vector<int>& capacity, vector<int>& rocks, int additionalRocks) {
// The number of items that can be put into each backpack , The same type of put number can only be saved and put once
vector<int> spaceVec;
// key:spaceVec The value in
// value : The number of backpacks put in this number
map<int, int> map;
int ans = 0;
// Traverse all backpacks , Record empty remainder
for(int i = 0; i < capacity.size(); i++){
// Backpack size
int cap = capacity[i];
// The number of backpacks has been put
int roc = rocks[i];
if(cap > roc){
// The backpack can also be put in quantity
int tmp = cap - roc;
auto it = map.find(tmp);
if(it == end(map)){
// Not recorded
spaceVec.push_back(tmp);
map.emplace(tmp, 1);
}else{
// It is recorded that only the quantity is increased by one
map[tmp] = map[tmp] + 1;
}
}else if(roc == cap){
// The backpack itself is full
ans++;
}
}
// In order to get the maximum number of full backpacks
// There will be Number that can be put in Sort by increments
sort(begin(spaceVec), end(spaceVec));
// Number of objects available
int avail = additionalRocks;
// From small to large Number that can be put in Containers
for(int i = 0; i < spaceVec.size(); i++){
// Number that can be put in
int roc = spaceVec[i];
// Change the number of occurrences of the number that can be put in
int times = map[roc];
while(times-- > 0){
// When the utilization object is smaller than 0 yes , Directly return the current result
if(0 > (avail -= roc)){
return ans;
}
// If it is satisfied, the result will be accumulated
ans++;
}
}
return ans;
}
};
边栏推荐
- 语义分割学习笔记(一)
- [leetcode] 877 stone game
- 2278. 字母在字符串中的百分比
- Build your own semantic segmentation platform deeplabv3+
- 11_ Redis_ Hyperloglog_ command
- Bing. Com website
- [leetcode] 1254 - count the number of closed Islands
- Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
- MySQL calculate n-day retention rate
- Semantic segmentation learning notes (1)
猜你喜欢
Leetcode skimming -- count the number of numbers with different numbers 357 medium
Semantic segmentation learning notes (1)
LeetCode刷题——验证二叉树的前序序列化#331#Medium
【LeetCode】1905-统计子岛屿
Leetcode skimming - remove duplicate letters 316 medium
Pytoch saves tensor to Mat file
Evaluation of embedded rz/g2l processor core board and development board of Feiling
How to intercept the value of a key from the JSON string returned by wechat?
Engineer evaluation | rk3568 development board hands-on test
MySQL calculate n-day retention rate
随机推荐
Bing.com网站
18_ Redis_ Redis master-slave replication & cluster building
College entrance examination score line climbing
Leetcode skimming - remove duplicate letters 316 medium
提前批院校名称
【Leetcode】167-两数之和II -输入有序数组
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
Oracle primary key auto increment
2278. 字母在字符串中的百分比
2303. 计算应缴税款总额
I made an istio workshop. This is the first introduction
How to intercept the value of a key from the JSON string returned by wechat?
密码学基础知识
【LeetCode】200-岛屿数量
Engineer evaluation | rk3568 development board hands-on test
【LeetCode】877-石子游戏
14_ Redis_ Optimistic lock
党史纪实主题公益数字文创产品正式上线
Force deduction solution summary 2029 stone game IX
yolo格式数据集处理(xml转txt)