Total Cost to Hire K Workers

You are given an integer array representing the cost to hire each worker. The goal is to hire exactly k workers in k sessions according to specific hiring rules. You choose workers with the lowest cost from either the first or last group of candidates, resolving ties by choosing the smallest index. If fewer than candidates remain, hire from these remaining workers at the lowest cost. Each worker is hired once, and the goal is to calculate the total cost to hire exactly k workers.

Constraints

  • 1 <= costs.length <= 10^5
  • 1 <= costs[i] <= 10^5
  • 1 <= k, candidates <= costs.length
Auto-saved · 2 lines
class Solution:
def totalCostToHireKWorkers(self, costs, k, candidates) -> int:
Console

Click Run to execute sample tests.