题目如下
数据范围
示例
显然我们应该使用最少的石头装满最多的背包,典型的贪心思想。本题仅需将capacity与rocks的差值算出来排序就行。
通过代码
cpp
class Solution {
public:
int maximumBags(vector<int>& capacity, vector<int>& rocks, int additionalRocks) {
int n = capacity.size();
int count = 0;
vector<int> c;
for(int i = 0;i < n;i++) {
c.push_back(capacity[i] - rocks[i]);
}
sort(c.begin(), c.end());
for(int &t :c) {
if(additionalRocks >= t) {
additionalRocks -= t;
count++;
}else {
break;
}
}
return count;
}
};