当前位置:网站首页>2279. 装满石头的背包的最大数量
2279. 装满石头的背包的最大数量
2022-07-02 12:10:00 【紫菜(Nori)】
class Solution {
public:
int maximumBags(vector<int>& capacity, vector<int>& rocks, int additionalRocks) {
// 存放每个背包还可放入物件数,相同类型的放入数只保存放入一次
vector<int> spaceVec;
// key:spaceVec中的值
// value : 这种放入数的背包个数
map<int, int> map;
int ans = 0;
// 遍历所有背包,记录空余数
for(int i = 0; i < capacity.size(); i++){
// 背包大小
int cap = capacity[i];
// 背包已放入数量
int roc = rocks[i];
if(cap > roc){
// 背包还可放入数量
int tmp = cap - roc;
auto it = map.find(tmp);
if(it == end(map)){
// 没有记录过
spaceVec.push_back(tmp);
map.emplace(tmp, 1);
}else{
// 记录过只将数量加一
map[tmp] = map[tmp] + 1;
}
}else if(roc == cap){
// 背包本身已经满
ans++;
}
}
// 为了获取最大的满背包数量
// 这里将 可放入数 进行递增排序
sort(begin(spaceVec), end(spaceVec));
// 可利用的物件数
int avail = additionalRocks;
// 从小到大遍历 可放入数 容器
for(int i = 0; i < spaceVec.size(); i++){
// 可放入数
int roc = spaceVec[i];
// 改数量可放入数的出现次数
int times = map[roc];
while(times-- > 0){
// 当利用物件小于0是,直接返回当前结果
if(0 > (avail -= roc)){
return ans;
}
// 满足放入则累加结果
ans++;
}
}
return ans;
}
};边栏推荐
- 搭建自己的语义分割平台deeplabV3+
- [leetcode] 876 intermediate node of linked list
- FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- [leetcode] 189 rotation array
- Build your own semantic segmentation platform deeplabv3+
- 【LeetCode】695-岛屿的最大面积
- 07_ Hash
- [leetcode] 1020 number of enclaves
- JVM architecture, classloader, parental delegation mechanism
猜你喜欢

18_ Redis_ Redis master-slave replication & cluster building

2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)

Oracle primary key auto increment

搭建自己的语义分割平台deeplabV3+

Leetcode skimming -- count the number of numbers with different numbers 357 medium

语义分割学习笔记(一)

Markdown tutorial

Solution of Queen n problem

Basic knowledge of cryptography

Data analysis thinking analysis methods and business knowledge - business indicators
随机推荐
Summary of the first three passes of sqli Labs
高考录取分数线爬虫
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
【LeetCode】876-链表的中间结点
[leetcode] 486 predict winners
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
【LeetCode】417-太平洋大西洋水流问题
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
Oracle primary key auto increment
For the problem that Folium map cannot be displayed, the temporary solution is as follows
【LeetCode】1140-石子游戏II
PTA ladder game exercise set l2-001 inter city emergency rescue
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
【LeetCode】344-反转字符串
【Leetcode】167-两数之和II -输入有序数组
SQL stored procedure
Wechat Alipay account system and payment interface business process
10_Redis_geospatial_命令