当前位置:网站首页>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;
}
};
边栏推荐
- 13_ Redis_ affair
- FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
- 怎样从微信返回的json字符串中截取某个key的值?
- Force deduction solution summarizes the lucky numbers in 1380 matrix
- 20_ Redis_ Sentinel mode
- [leetcode] 1254 - count the number of closed Islands
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- yolo格式数据集处理(xml转txt)
- Markdown tutorial
- QML pop-up frame, customizable
猜你喜欢
Bing.com网站
Oracle primary key auto increment
How to intercept the value of a key from the JSON string returned by wechat?
【LeetCode】417-太平洋大西洋水流问题
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
Leetcode skimming - remove duplicate letters 316 medium
Case introduction and problem analysis of microservice
LeetCode刷题——去除重复字母#316#Medium
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
随机推荐
MD5加密
04.进入云原生后的企业级应用构建的一些思考
College entrance examination admission score line climbing
. Net again! Happy 20th birthday
[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
List of sergeant schools
16_ Redis_ Redis persistence
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
Data analysis thinking analysis methods and business knowledge - business indicators
Bing. Com website
【LeetCode】200-岛屿数量
Bing.com网站
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
Markdown tutorial
YOLOV5 代码复现以及搭载服务器运行