class Solution {
typedef long long ll;
const int MOD = 1e9 + 7;
public:
int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
vector<pair<ll,ll>>tmp;
for(int i = 0; i < n; i++)
{
tmp.push_back(make_pair(efficiency[i], speed[i]));
}
sort(tmp.rbegin(), tmp.rend()); // == sort(tmp.begin(), tmp.end(), greater<pair<ll,ll>()); 即降序
priority_queue<ll, vector<ll>, greater<ll>> pq; //小根堆
ll sum = 0, best = 0;
for(int i = 0; i < n; i++)
{
pq.push(tmp[i].second);
sum += tmp[i].second;
if((int)pq.size() > k)
{
sum -= pq.top();
pq.pop();
}
best = max(best, sum * tmp[i].first); // tmp[i].first是0~i中效率最小的元素,注意题目说的是最多k个
}
best %= MOD;
return best;
}
};
[leetcode]5359. 最大的团队表现值
最新推荐文章于 2020-05-29 16:47:46 发布