-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path2462. Total Cost to Hire K Workers
63 lines (58 loc) · 2.4 KB
/
2462. Total Cost to Hire K Workers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution {
public:
long long totalCost(vector<int>& costs, int k, int candidates) {
long long ans = 0; // Variable to store the total cost
priority_queue<int, vector<int>, greater<int>> q1; // Priority queue to store the lowest costs from the beginning of the array
priority_queue<int, vector<int>, greater<int>> q2; // Priority queue to store the highest costs from the end of the array
int low = 0; // Pointer to track the beginning of the array
// Insert 'candidates' number of costs into q1
while (low < candidates) {
q1.push(costs[low]);
low++;
}
int cnt = 0;
int high = (int)costs.size() - 1; // Pointer to track the end of the array
// Adjust 'candidates' if it is greater than the number of elements from the end of the array
if (candidates > costs.size() - candidates) {
candidates = costs.size() - candidates;
}
// Insert 'candidates' number of costs into q2 from the end of the array
while (cnt < candidates) {
q2.push(costs[high]);
cnt++;
high--;
}
while (k--) {
if (q1.empty()) { // If q1 is empty, take the lowest cost from q2
ans += q2.top();
q2.pop();
if (low <= high) {
q2.push(costs[high]);
high--;
}
} else if (q2.empty()) { // If q2 is empty, take the lowest cost from q1
ans += q1.top();
q1.pop();
if (low <= high) {
q1.push(costs[low]);
low++;
}
} else if (q1.top() <= q2.top()) { // If the lowest cost in q1 is less than or equal to the lowest cost in q2, take the lowest cost from q1
ans += q1.top();
q1.pop();
if (low <= high) {
q1.push(costs[low]);
low++;
}
} else { // If the lowest cost in q2 is less than the lowest cost in q1, take the lowest cost from q2
ans += q2.top();
q2.pop();
if (low <= high) {
q2.push(costs[high]);
high--;
}
}
}
return ans; // Return the total cost
}
};