Leetcode 887 Solution

This article provides solution to leetcode question 887 (minimum-cost-to-hire-k-workers)

https://leetcode.com/problems/minimum-cost-to-hire-k-workers

Solution

class Solution(object): def mincostToHireWorkers(self, quality, wage, K): """ :type quality: List[int] :type wage: List[int] :type K: int :rtype: float """ a = sorted([(w * 1.0 / q, w, q) for w, q in zip(wage, quality)])
heap = [] heap_sum = 0 ans = sys.maxint
for r, w, q in a: heapq.heappush(heap, -q) heap_sum += q
if len(heap) == K: ans = min(ans, heap_sum * r) heap_sum += heapq.heappop(heap)
return ans