leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2542.cpp (703B)


      1 class Solution {
      2     typedef pair<long long, long long> elem;
      3 
      4   public:
      5     long long maxScore(vector<int> &nums1, vector<int> &nums2, int k) {
      6         int n = nums1.size();
      7         vector<elem> arr(n);
      8         for (int i = 0; i < n; ++i)
      9             arr[i] = {nums2[i], nums1[i]};
     10         sort(rbegin(arr), rend(arr));
     11 
     12         long long sum = 0, res = 0;
     13         priority_queue<int, vector<int>, greater<int>> pq;
     14         for (auto &[a, b] : arr) {
     15             pq.emplace(b);
     16             sum += b;
     17             if (pq.size() > k) {
     18                 sum -= pq.top();
     19                 pq.pop();
     20             }
     21             if (pq.size() == k) res = max(res, sum * a);
     22         }
     23         return res;
     24     }
     25 };