-
Notifications
You must be signed in to change notification settings - Fork 0
/
26.6.23(2462. Total Cost to Hire K Workers(Medium))
65 lines (58 loc) · 1.6 KB
/
26.6.23(2462. Total Cost to Hire K Workers(Medium))
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
64
65
class Solution {
public:
long long totalCost(vector<int>& cost, int k, int take)
{
typedef pair<int,int>p;
int n = cost.size();
//if 2*take >= n then we will simply push everything into a pq then run it for k times
long long ans = 0;
priority_queue<p,vector<p>,greater<p>>pq;
if(2*take >= n)
{
for(auto i : cost)
{
pq.push({i,0});
}
while(k--)
{
auto it = pq.top();
ans += it.first;
pq.pop();
}
return ans;
}
else
{
int i,j;
//from front
int temp = take;
for(i = 0; temp--;i++)
{
pq.push({cost[i],0}); // from front as indicator push 0;
}
i--;
//from back
temp = take;
for(j = n-1;temp--;j--)
{
pq.push({cost[j],1}); // from back as indicator push 1;
}
j++;
bool flag = false ;
while(pq.size() && k--)
{
auto it = pq.top();
pq.pop();
int c = it.first; // c --> cost[i]
int ind = it.second; // ind --> indicator that it from first or last
ans += c;
(!flag && ind == 0)? i++ : j--;
if(i >= j)
flag = true;
else
(ind == 0)? pq.push({cost[i],0}) : pq.push({cost[j],1});
}
return ans;
}
}
};