当前位置:网站首页>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;
}
};边栏推荐
- Beijing rental data analysis
- Bing.com网站
- How to avoid 7 common problems in mobile and network availability testing
- 搭建自己的语义分割平台deeplabV3+
- MySQL calculate n-day retention rate
- How to find a sense of career direction
- 工程师评测 | RK3568开发板上手测试
- 17_ Redis_ Redis publish subscription
- Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
- LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
猜你喜欢

03. Preliminary use of golang

Wechat Alipay account system and payment interface business process

微信支付宝账户体系和支付接口业务流程
![[leetcode] 1254 - count the number of closed Islands](/img/84/f888ae0e164951cd9623fb3bf4a984.png)
[leetcode] 1254 - count the number of closed Islands

10_ Redis_ geospatial_ command

(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice

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

百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香

Bing.com網站

终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
随机推荐
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
How to find a sense of career direction
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
folium,确诊和密接轨迹上图
Redux - detailed explanation
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
Party History Documentary theme public welfare digital cultural and creative products officially launched
[leetcode] 1162 map analysis
高考录取分数线爬虫
[leetcode] 417 - Pacific Atlantic current problem
Leetcode skimming -- sum of two integers 371 medium
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
LeetCode刷题——两整数之和#371#Medium
[leetcode] 344 reverse string
[leetcode] 695 - maximum area of the island
【LeetCode】19-删除链表的倒数第N个结点
【LeetCode】344-反转字符串
搭建自己的语义分割平台deeplabV3+
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统