当前位置:网站首页>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;
}
};边栏推荐
- MD5 encryption
- 终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
- [leetcode] 1140 stone game II
- Bing. Site Internet
- [leetcode] 577 reverse word III in string
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- LeetCode刷题——奇偶链表#328#Medium
- Markdown tutorial
- LeetCode刷题——去除重复字母#316#Medium
猜你喜欢

18_ Redis_ Redis master-slave replication & cluster building

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测

Custom exception

5. Practice: jctree implements the annotation processor at compile time

Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board

03. Preliminary use of golang

FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)

Leetcode skimming -- incremental ternary subsequence 334 medium

PTA 天梯赛习题集 L2-001 城市间紧急救援

Leetcode question brushing - parity linked list 328 medium
随机推荐
11_ Redis_ Hyperloglog_ command
Bing. Site Internet
Wechat Alipay account system and payment interface business process
[leetcode] 876 intermediate node of linked list
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
Summary of the first three passes of sqli Labs
【LeetCode】1254-统计封闭岛屿的数量
LeetCode_ String_ Simple_ 412.Fizz Buzz
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
Solve the problem of frequent interruption of mobaxterm remote connection
[leetcode] 19 delete the penultimate node of the linked list
Cultural scores of summer college entrance examination
提前批院校名称
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
高考录取分数线爬虫
Download blender on Alibaba cloud image station
College entrance examination admission score line crawler
College entrance examination score line climbing
密码学基础知识
03. Preliminary use of golang