Skip to content

Commit 1ba8bbe

Browse files
committed
2462. Total Cost to Hire K Workers
1 parent 85e05fd commit 1ba8bbe

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@
231231
| 2413 | Smallest Even Multiple | [Ruby](./algorithms/ruby/2413-smallest-even-multiple.rb) | Easy |
232232
| 2439 | Minimize Maximum of Array | [Ruby](./algorithms/ruby/2439-minimize-maximum-of-array.rb) | Medium |
233233
| 2444 | Count Subarrays With Fixed Bounds | [Ruby](./algorithms/ruby/2444-count-subarrays-with-fixed-bounds.rb) | Hard |
234+
| 2462 | Total Cost to Hire K Workers | [Python3](./algorithms/python3/2462-total-cost-to-hire-k-workers.py) | Medium |
234235
| 2466 | Count Ways To Build Good Strings | [Ruby](./algorithms/ruby/2466-count-ways-to-build-good-strings.rb) | Medium |
235236
| 2477 | Minimum Fuel Cost to Report to the Capital | [Ruby](./algorithms/ruby/2477-minimum-fuel-cost-to-report-to-the-capital.rb) | Medium |
236237
| 2492 | Minimum Score of a Path Between Two Cities | [Ruby](./algorithms/ruby/2492-minimum-score-of-a-path-between-two-cities.rb) | Medium |
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 2462. Total Cost to Hire K Workers
2+
# https://leetcode.com/problems/total-cost-to-hire-k-workers
3+
# Medium
4+
5+
from heapq import heapify, heappush, heappop
6+
7+
class Solution:
8+
def totalCost(self, costs: list[int], k: int, candidates: int) -> int:
9+
q = costs[:candidates]
10+
qq = costs[max(candidates, len(costs)-candidates):]
11+
heapify(q)
12+
heapify(qq)
13+
ans = 0
14+
i, ii = candidates, len(costs)-candidates-1
15+
for _ in range(k):
16+
if not qq or q and q[0] <= qq[0]:
17+
ans += heappop(q)
18+
if i <= ii:
19+
heappush(q, costs[i])
20+
i += 1
21+
else:
22+
ans += heappop(qq)
23+
if i <= ii:
24+
heappush(qq, costs[ii])
25+
ii -= 1
26+
return ans
27+
28+
# ********************#
29+
# TEST #
30+
# ********************#
31+
32+
import unittest
33+
34+
class TestMinimumTime(unittest.TestCase):
35+
def test_minimumTime(self):
36+
self.assertEqual(Solution.totalCost(self, [17,12,10,2,7,2,11,20,8], 3, 4), 11)
37+
self.assertEqual(Solution.totalCost(self, [1,2,4,1], 3, 3), 4)
38+
39+
if __name__ == '__main__':
40+
unittest.main()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
# 2462. Total Cost to Hire K Workers
4+
# https://leetcode.com/problems/total-cost-to-hire-k-workers
5+
# Medium
6+
# TODO: Implement
7+
8+
=begin
9+
You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the ith worker.
10+
11+
You are also given two integers k and candidates. We want to hire exactly k workers according to the following rules:
12+
13+
You will run k sessions and hire exactly one worker in each session.
14+
In each hiring session, choose the worker with the lowest cost from either the first candidates workers or the last candidates workers. Break the tie by the smallest index.
15+
For example, if costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, we will choose the 4th worker because they have the lowest cost [3,2,7,7,1,2].
16+
In the second hiring session, we will choose 1st worker because they have the same lowest cost as 4th worker but they have the smallest index [3,2,7,7,2]. Please note that the indexing may be changed in the process.
17+
If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.
18+
A worker can only be chosen once.
19+
Return the total cost to hire exactly k workers.
20+
21+
Example 1:
22+
Input: costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
23+
Output: 11
24+
Explanation: We hire 3 workers in total. The total cost is initially 0.
25+
- In the first hiring round we choose the worker from [17,12,10,2,7,2,11,20,8]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
26+
- In the second hiring round we choose the worker from [17,12,10,7,2,11,20,8]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
27+
- In the third hiring round we choose the worker from [17,12,10,7,11,20,8]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
28+
The total hiring cost is 11.
29+
30+
Example 2:
31+
Input: costs = [1,2,4,1], k = 3, candidates = 3
32+
Output: 4
33+
Explanation: We hire 3 workers in total. The total cost is initially 0.
34+
- In the first hiring round we choose the worker from [1,2,4,1]. 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.
35+
- In the second hiring round we choose the worker from [2,4,1]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
36+
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [2,4]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
37+
The total hiring cost is 4.
38+
39+
Constraints:
40+
1 <= costs.length <= 105
41+
1 <= costs[i] <= 105
42+
1 <= k, candidates <= costs.length
43+
=end
44+
45+
# @param {Integer[]} costs
46+
# @param {Integer} k
47+
# @param {Integer} candidates
48+
# @return {Integer}
49+
def total_cost(costs, k, candidates)
50+
end
51+
52+
# ********************#
53+
# TEST #
54+
# ********************#
55+
56+
require "test/unit"
57+
class Test_total_cost < Test::Unit::TestCase
58+
def test_
59+
assert_equal 11, total_cost([17,12,10,2,7,2,11,20,8], 3, 4)
60+
assert_equal 4, total_cost([1,2,4,1], 3, 3)
61+
end
62+
end

0 commit comments

Comments
 (0)