当前位置:网站首页>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;
}
};边栏推荐
猜你喜欢

Redux——详解

19_ Redis_ Manually configure the host after downtime

Beijing rental data analysis

Bing.com網站

已知两种遍历序列构造二叉树

搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!

LeetCode刷题——统计各位数字都不同的数字个数#357#Medium

How to find a sense of career direction

SQL stored procedure

FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
随机推荐
[leetcode] 486 predict winners
How to intercept the value of a key from the JSON string returned by wechat?
03.golang初步使用
(4) Flink's table API and SQL table schema
[leetcode] 1140 stone game II
2278. 字母在字符串中的百分比
微信支付宝账户体系和支付接口业务流程
【LeetCode】977-有序數組的平方
搭建自己的语义分割平台deeplabV3+
College entrance examination admission score line climbing
密码学基础知识
【LeetCode】876-链表的中间结点
高考分数线爬取
Force deduction solution summarizes the lucky numbers in 1380 matrix
yolo格式数据集处理(xml转txt)
LeetCode刷题——去除重复字母#316#Medium
Build your own semantic segmentation platform deeplabv3+
工程师评测 | RK3568开发板上手测试
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
16_ Redis_ Redis persistence