## 2462. Total Cost to Hire K Workers https://leetcode.com/problems/total-cost-to-hire-k-workers/ ## Two heaps - Idea - using two min heap from both side - Complexity - Time: O(NlogN + klogN), N is the # of candidates, k means # of hiring rounds - Space: O(N) ```= class Solution { public: long long totalCost(vector<int>& costs, int k, int candidates) { int l = 0, r = costs.size() - 1; priority_queue<int, vector<int>, greater<int>> lPq; priority_queue<int, vector<int>, greater<int>> rPq; long long cost = 0; while(k--){ while(lPq.size() < candidates and l <= r){ lPq.push(costs[l++]); } while(rPq.size() < candidates and l <= r){ rPq.push(costs[r--]); } int pick1 = lPq.size() > 0 ? lPq.top() : INT_MAX; int pick2 = rPq.size() > 0 ? rPq.top() : INT_MAX; if (pick1 <= pick2){ cost += pick1; lPq.pop(); } else{ cost += pick2; rPq.pop(); } } return cost; } }; ```