当前位置:网站首页>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;
}
};边栏推荐
- 6096. 咒语和药水的成功对数
- Case introduction and problem analysis of microservice
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- LeetCode刷题——奇偶链表#328#Medium
- 做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
- 03. Preliminary use of golang
- 2279. 装满石头的背包的最大数量
- MD5 encryption
- 【LeetCode】877-石子游戏
- 18_ Redis_ Redis master-slave replication & cluster building
猜你喜欢

工程师评测 | RK3568开发板上手测试

10_Redis_geospatial_命令

Leetcode skimming - remove duplicate letters 316 medium

LeetCode刷题——递增的三元子序列#334#Medium

PTA ladder game exercise set l2-001 inter city emergency rescue

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

SQL stored procedure

Leetcode skimming -- incremental ternary subsequence 334 medium

20_ Redis_ Sentinel mode

6.12 企业内部upp平台(Unified Process Platform)的关键一刻
随机推荐
SQL stored procedure
MySQL calculate n-day retention rate
密码学基础知识
folium地图无法显示的问题,临时性解决方案如下
. Net again! Happy 20th birthday
2303. 计算应缴税款总额
04.进入云原生后的企业级应用构建的一些思考
【LeetCode】283-移动零
How to avoid 7 common problems in mobile and network availability testing
Bing.com网站
College entrance examination admission score line crawler
党史纪实主题公益数字文创产品正式上线
【LeetCode】876-链表的中间结点
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
【LeetCode】1140-石子游戏II
5. Practice: jctree implements the annotation processor at compile time
[leetcode] 977 square of ordered array
Leetcode skimming -- sum of two integers 371 medium
15_ Redis_ Redis. Conf detailed explanation