leetcode

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

2462.cpp (757B)


0 class Solution { 1 public: 2 long long totalCost(vector<int> &costs, int k, int candidates) { 3 priority_queue<int, vector<int>, greater<int>> pq1, pq2; 4 int i = 0, j = costs.size() - 1; 5 long long res = 0; 6 while (k--) { 7 while (pq1.size() < candidates && i <= j) 8 pq1.push(costs[i++]); 9 while (pq2.size() < candidates && j >= i) 10 pq2.push(costs[j--]); 11 int a = pq1.size() > 0 ? pq1.top() : INT_MAX; 12 int b = pq2.size() > 0 ? pq2.top() : INT_MAX; 13 if (a <= b) { 14 res += a; 15 pq1.pop(); 16 } else { 17 res += b; 18 pq2.pop(); 19 } 20 } 21 return res; 22 } 23 };