Tap here to switch tabs
Problem
Submissions

Problem: Total Cost to Hire K Workers

med
30 min
Try to solve the Total Cost to Hire K Workers problem.

Statement

You are given an integer array costs, where costs[i] represents the hiring cost of worker i.

You are also given two integers k and candidates. You want to hire exactly k workers according to the following rules:

In each of the k hiring sessions, you choose the worker with the minimum cost from either the first candidates available workers or the last candidates available workers. If there is a tie with the same minimum cost, choose the worker with the smallest index. If the number of remaining workers is strictly less than candidates, choose the worker with the minimum cost among all remaining workers, breaking ties by the smallest index.

Each chosen worker is removed from the array and cannot be selected in subsequent sessions.

Return the total cost required to hire k workers.

Constraints:

  • 1≤1 \leq costs.length ≤105\leq 10^5

  • 1≤1 \leq costs[i] ≤105\leq 10^5

  • 1≤1 \leq k, candidates ≤\leq costs.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Total Cost to Hire K Workers

med
30 min
Try to solve the Total Cost to Hire K Workers problem.

Statement

You are given an integer array costs, where costs[i] represents the hiring cost of worker i.

You are also given two integers k and candidates. You want to hire exactly k workers according to the following rules:

In each of the k hiring sessions, you choose the worker with the minimum cost from either the first candidates available workers or the last candidates available workers. If there is a tie with the same minimum cost, choose the worker with the smallest index. If the number of remaining workers is strictly less than candidates, choose the worker with the minimum cost among all remaining workers, breaking ties by the smallest index.

Each chosen worker is removed from the array and cannot be selected in subsequent sessions.

Return the total cost required to hire k workers.

Constraints:

  • 1≤1 \leq costs.length ≤105\leq 10^5

  • 1≤1 \leq costs[i] ≤105\leq 10^5

  • 1≤1 \leq k, candidates ≤\leq costs.length