当前位置:网站首页>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;
}
};
边栏推荐
- [leetcode] 695 - maximum area of the island
- Leetcode skimming -- incremental ternary subsequence 334 medium
- 【LeetCode】876-链表的中间结点
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- 2303. 计算应缴税款总额
- [leetcode] 877 stone game
- Redux——详解
- 【LeetCode】1140-石子游戏II
- 语义分割学习笔记(一)
- 07_ Hash
猜你喜欢
密码学基础知识
[leetcode] 417 - Pacific Atlantic current problem
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
16_ Redis_ Redis persistence
YOLOV5 代码复现以及搭载服务器运行
Semantic segmentation learning notes (1)
微信支付宝账户体系和支付接口业务流程
自定义异常
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
XML Configuration File
随机推荐
[leetcode] 1162 map analysis
Solve the problem of frequent interruption of mobaxterm remote connection
【LeetCode】283-移动零
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
[leetcode] 200 number of islands
How does the computer set up speakers to play microphone sound
19_ Redis_ Manually configure the host after downtime
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
【LeetCode】1162-地图分析
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
14_ Redis_ Optimistic lock
Redux——详解
Yolov5 code reproduction and server operation
PTA 天梯赛习题集 L2-001 城市间紧急救援
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
密码学基础知识
飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
[solution] educational codeforces round 82
I made an istio workshop. This is the first introduction
[leetcode] 283 move zero