[leetcode]5359. 最大的團隊表現值

在這裏插入圖片描述

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;
    }
};

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章