100233. 重新分装苹果
排序,然后往箱子里面放。
cpp
#include <bits/stdc++.h>
//#define int long long
#define per(i,j,k) for(int (i)=(j);(i)<=(k);++(i))
#define rep(i,j,k) for(int (i)=(j);(i)>=(k);--(i))
#define debug(a) cout<<#a<<"="<<a<<endl
#define all(x) x.begin(),x.end()
#define fr first
#define se second
#define endl '\n'
using namespace std;
class Solution{
public:
int minimumBoxes(vector<int>& apple, vector<int>& capacity) {
sort(all(capacity),[](int a,int b){
return a>b;
});
int now=0;
per(i,0,apple.size()-1){
if(apple[i]<=capacity[now]){
capacity[now]-=apple[i];
}else{
apple[i]-=capacity[now];
now++;
i--;
}
}
return now+1;
}
};
100247. 幸福值最大化的选择方案
在序列中选数,每选一个,其他所有数减一。问最终能选出最大的总和。
也是排序然后模拟。
cpp
class Solution {
public:
long long maximumHappinessSum(vector<int>& happiness, int k) {
sort(happiness.begin(),happiness.end(),[](int a,int b){
return a>b;
});
long long ans=0,cnt=-1;
for(int i=0;i<happiness.size() and k;++i){
cnt++;
k--;
if(happiness[i]-cnt<=0)break;
ans+=happiness[i]-cnt;
}
return ans;
}
};