当前位置:网站首页>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;
}
};边栏推荐
- 【Salesforce】如何确认你的Salesforce版本?
- Engineer evaluation | rk3568 development board hands-on test
- How to find a sense of career direction
- [leetcode] 283 move zero
- Download blender on Alibaba cloud image station
- Leetcode skimming - remove duplicate letters 316 medium
- Loss function and positive and negative sample allocation: Yolo series
- 10_ Redis_ geospatial_ command
- Evaluation of embedded rz/g2l processor core board and development board of Feiling
- 18_ Redis_ Redis master-slave replication & cluster building
猜你喜欢

6.12 企业内部upp平台(Unified Process Platform)的关键一刻

LeetCode刷题——验证二叉树的前序序列化#331#Medium

【LeetCode】1254-统计封闭岛屿的数量

Pytoch saves tensor to Mat file

【LeetCode】417-太平洋大西洋水流问题

Custom exception

Oracle primary key auto increment

Bing. Com website

Build your own semantic segmentation platform deeplabv3+

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
随机推荐
10_ Redis_ geospatial_ command
【LeetCode】1905-统计子岛屿
【LeetCode】486-预测赢家
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
Evaluation of embedded rz/g2l processor core board and development board of Feiling
18_ Redis_ Redis master-slave replication & cluster building
彻底弄懂浏览器强缓存和协商缓存
【LeetCode】695-岛屿的最大面积
【LeetCode】977-有序数组的平方
yolo格式数据集处理(xml转txt)
20_ Redis_ Sentinel mode
密码学基础知识
Steps for Navicat to create a new database
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
LeetCode刷题——去除重复字母#316#Medium
folium,确诊和密接轨迹上图
6.12 critical moment of Unified Process Platform
【LeetCode】283-移动零
Loss function and positive and negative sample allocation: Yolo series
【LeetCode】977-有序數組的平方