当前位置:网站首页>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;
}
};
边栏推荐
- [network security] network asset collection
- 【Salesforce】如何确认你的Salesforce版本?
- (Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
- [leetcode] 695 - maximum area of the island
- Yolo format data set processing (XML to txt)
- 2278. 字母在字符串中的百分比
- LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
- 6095. 强密码检验器 II
- LeetCode刷题——验证二叉树的前序序列化#331#Medium
- [leetcode] 977 - carré du tableau ordonné
猜你喜欢
5. Practice: jctree implements the annotation processor at compile time
LeetCode刷题——递增的三元子序列#334#Medium
党史纪实主题公益数字文创产品正式上线
19_ Redis_ Manually configure the host after downtime
彻底弄懂浏览器强缓存和协商缓存
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
工程师评测 | RK3568开发板上手测试
Wechat Alipay account system and payment interface business process
Semantic segmentation learning notes (1)
Bing. Site Internet
随机推荐
【LeetCode】877-石子游戏
10_ Redis_ geospatial_ command
MySQL -- Index Optimization -- order by
[leetcode] 1254 - count the number of closed Islands
Markdown tutorial
自定义异常
5. Practice: jctree implements the annotation processor at compile time
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
Real estate market trend outlook in 2022
4. Data splitting of Flink real-time project
Semantic segmentation learning notes (1)
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
LeetCode刷题——两整数之和#371#Medium
Yolo format data set processing (XML to txt)
Basic knowledge of cryptography
彻底弄懂浏览器强缓存和协商缓存
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
【LeetCode】1254-统计封闭岛屿的数量
02. After containerization, you must face golang