java.lang.Object
g2401_2500.s2462_total_cost_to_hire_k_workers.Solution

public class Solution extends Object
2462 - Total Cost to Hire K Workers.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p> <p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p> <ul> <li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li> <li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index. <ul> <li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<ins>3,2</ins>,7,7,<ins> <strong>1</strong> ,2</ins>]</code>.</li> <li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<ins>3, <strong>2</strong> </ins>,7,<ins>7,2</ins>]</code>. Please note that the indexing may be changed in the process.</li> </ul> </li> <li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li> <li>A worker can only be chosen once.</li> </ul> <p>Return <em>the total cost to hire exactly</em> <code>k</code> <em>workers.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4</p> <p><strong>Output:</strong> 11</p> <p><strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.</p> <ul> <li> <p>In the first hiring round we choose the worker from [<ins>17,12,10,2</ins>,7,<ins>2,11,20,8</ins>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.</p> </li> <li> <p>In the second hiring round we choose the worker from [<ins>17,12,10,7</ins>,<ins>2,11,20,8</ins>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.</p> </li> <li> <p>In the third hiring round we choose the worker from [<ins>17,12,10,7,11,20,8</ins>]. The lowest cost is 7 (index 3).</p> </li> </ul> <p>The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.</p> <p>The total hiring cost is 11.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.</p> <ul> <li> <p>In the first hiring round we choose the worker from [<ins>1,2,4,1</ins>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.</p> </li> <li> <p>In the second hiring round we choose the worker from [<ins>2,4,1</ins>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.</p> </li> <li> <p>In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<ins>2,4</ins>]. The lowest cost is 2 (index 0).</p> </li> </ul> <p>The total cost = 2 + 2 = 4. The total hiring cost is 4.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= costs.length <= 10<sup>5</sup></code></li> <li><code>1 <= costs[i] <= 10<sup>5</sup></code></li> <li><code>1 <= k, candidates <= costs.length</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • totalCost

      public long totalCost(int[] costs, int k, int candidates)