2462. Total Cost to Hire K Workers - Day 26/30 Leetcode June Challenge
ฝัง
- เผยแพร่เมื่อ 5 ก.พ. 2025
- Larry solves and analyzes this Leetcode problem as both an interviewer and an interviewee. This is a live recording of a real engineer solving a problem live - no cuts or edits!
Problem: leetcode.com/p...
Twitch: / larryny
Discord: / discord
Instagram: / larrysomewhere
#leetcode #coding #programming
Were you able to hire all the workers you need?
Thank u for this solution. I like ur videos because u provided the progress of thinking and try to debug.
that was great but its failing when
costs = [17, 12], k = 1, candidates = 1
I currently added the following static snippet to pass the test cases, but can you please check it once.
if k == candidates == 1:
return min(costs[0], costs[-1])
Ah, thanks for the catch! Wow what an edge case, haha, but the logic is still the same, you can change the code on line 14 to:
if left > right:
and it should resolve it, thanks!
hey larry, I came up with another solution but it is failing for some testcases. Can you help me identify what's wrong in the below code.
class Solution:
def totalCost(self, costs: List[int], k: int, candidates: int) -> int:
l = candidates-1
r = len(costs)-candidates
l1 = costs[:l+1:]
l2 = costs[r::]
heapq.heapify(l1)
heapq.heapify(l2)
total_cost = 0
for i in range(k):
l1_ele = heapq.heappop(l1)
l2_ele = heapq.heappop(l2)
if l1_ele l:
r-=1
heapq.heappush(l2, costs[r])
return total_cost