当前位置:网站首页>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;
}
};边栏推荐
- 02. After containerization, you must face golang
- 04.进入云原生后的企业级应用构建的一些思考
- [leetcode] 1162 map analysis
- 【LeetCode】577-反转字符串中的单词 III
- PTA ladder game exercise set l2-001 inter city emergency rescue
- MySQL calculate n-day retention rate
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- MD5 encryption
- 彻底弄懂浏览器强缓存和协商缓存
- SQL transaction
猜你喜欢

Bing. Site Internet

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
![[network security] network asset collection](/img/3e/6665b5af0dedfcbc7bd548cc486878.png)
[network security] network asset collection

Beijing rental data analysis
![[leetcode] 1254 - count the number of closed Islands](/img/84/f888ae0e164951cd9623fb3bf4a984.png)
[leetcode] 1254 - count the number of closed Islands

党史纪实主题公益数字文创产品正式上线

Case introduction and problem analysis of microservice

做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统

Basic knowledge of cryptography

How to intercept the value of a key from the JSON string returned by wechat?
随机推荐
【LeetCode】200-岛屿数量
List of sergeant schools
Download blender on Alibaba cloud image station
4. Jctree related knowledge learning
Markdown tutorial
Build your own semantic segmentation platform deeplabv3+
【LeetCode】977-有序數組的平方
PTA ladder game exercise set l2-001 inter city emergency rescue
Redux——详解
微信支付宝账户体系和支付接口业务流程
18_ Redis_ Redis master-slave replication & cluster building
【LeetCode】695-岛屿的最大面积
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Leetcode skimming -- sum of two integers 371 medium
MySQL calculate n-day retention rate
【LeetCode】283-移动零
11_Redis_Hyperloglog_命令
Oracle primary key auto increment
【LeetCode】577-反转字符串中的单词 III
Beijing rental data analysis