-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path857_minicost_kworker.py
More file actions
36 lines (32 loc) · 1.35 KB
/
Copy path857_minicost_kworker.py
File metadata and controls
36 lines (32 loc) · 1.35 KB
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
"""
There are N workers. The i-th worker has a quality[i] and a minimum wage expectation wage[i].
Now we want to hire exactly K workers to form a paid group. When hiring a group of K workers, we must pay them according to the following rules:
Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group.
Every worker in the paid group must be paid at least their minimum wage expectation.
Return the least amount of money needed to form a paid group satisfying the above conditions.
"""
class Solution:
def mincostToHireWorkers(self, quality, wage, K):
"""
:type quality: List[int]
:type wage: List[int]
:type K: int
:rtype: float
"""
uniwage = [x/y for x,y in zip(wage, quality)]
qw = list(zip(quality, uniwage))
qw_sort = sorted(qw, key=lambda s: s[1])
quality = [x[0] for x in qw_sort]
uniwage = [x[1] for x in qw_sort]
q = [x for x in quality[0:K]]
w = uniwage[K-1]
mincost = w * sum(q)
if len(uniwage) > K:
for i in range(K,len(uniwage)):
w = uniwage[i]
q.pop(q.index(max(q)))
q.append(quality[i])
cost = w * sum(q)
if cost < mincost:
mincost = cost
return(mincost)