当前位置:网站首页>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刷题——奇偶链表#328#Medium

【LeetCode】1254-统计封闭岛屿的数量

自定义异常

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测

Case introduction and problem analysis of microservice
![[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)](/img/cf/38e4035c3b318814672f21c8a42618.jpg)
[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)

Leetcode skimming -- incremental ternary subsequence 334 medium

Evaluation of embedded rz/g2l processor core board and development board of Feiling

Custom exception

How does the computer set up speakers to play microphone sound
随机推荐
folium地图无法显示的问题,临时性解决方案如下
Redux - detailed explanation
[leetcode] 167 - sum of two numbers II - enter an ordered array
[leetcode] 344 reverse string
04. Some thoughts on enterprise application construction after entering cloud native
Download blender on Alibaba cloud image station
Force deduction solution summarizes the lucky numbers in 1380 matrix
Infra11199 database system
MD5加密
[leetcode] 189 rotation array
Markdown tutorial
工程师评测 | RK3568开发板上手测试
LeetCode刷题——奇偶链表#328#Medium
I made an istio workshop. This is the first introduction
03.golang初步使用
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
Yolov5 code reproduction and server operation
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
[leetcode] 1254 - count the number of closed Islands