Skip to content

Commit d74337f

Browse files
committed
+ problem 857
1 parent b16f280 commit d74337f

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 857. Minimum Cost to Hire K Workers
2+
There are `n` workers. You are given two integer arrays `quality` and `wage` where `quality[i]` is the quality of the <code>i<sup>th</sup></code> worker and `wage[i]` is the minimum wage expectation for the <code>i<sup>th</sup></code> worker.
3+
4+
We want to hire exactly `k` workers to form a **paid group**. To hire a group of `k` workers, we must pay them according to the following rules:
5+
1. Every worker in the paid group must be paid at least their minimum wage expectation.
6+
2. In the group, each worker's pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.
7+
8+
Given the integer `k`, return *the least amount of money needed to form a paid group satisfying the above conditions*. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> quality = [10,20,5], wage = [70,50,30], k = 2
13+
<strong>Output:</strong> 105.00000
14+
<strong>Explanation:</strong> We pay 70 to 0th worker and 35 to 2nd worker.
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
20+
<strong>Output:</strong> 30.66667
21+
<strong>Explanation:</strong> We pay 4 to 0th worker, 13.33333 to 2nd and 3rd workers separately.
22+
</pre>
23+
24+
#### Constraints:
25+
* `n == quality.length == wage.length`
26+
* <code>1 <= k <= n <= 10<sup>4</sup></code>
27+
* <code>1 <= quality[i], wage[i] <= 10<sup>4</sup></code>
28+
29+
## Solutions (Rust)
30+
31+
### 1. Solution
32+
```Rust
33+
use std::collections::BinaryHeap;
34+
35+
impl Solution {
36+
pub fn mincost_to_hire_workers(quality: Vec<i32>, wage: Vec<i32>, k: i32) -> f64 {
37+
let k = k as usize;
38+
let mut workers = quality
39+
.into_iter()
40+
.zip(wage.into_iter())
41+
.collect::<Vec<_>>();
42+
let mut quality_heap = BinaryHeap::new();
43+
let mut quality_sum = 0;
44+
let mut ret = f64::INFINITY;
45+
46+
workers.sort_unstable_by(|(q0, w0), (q1, w1)| (q1 * w0).cmp(&(q0 * w1)));
47+
48+
for i in 0..workers.len() {
49+
if i >= k {
50+
quality_sum -= quality_heap.pop().unwrap();
51+
}
52+
53+
quality_heap.push(workers[i].0);
54+
quality_sum += workers[i].0;
55+
56+
if i >= k - 1 {
57+
ret = ret.min(quality_sum as f64 * workers[i].1 as f64 / workers[i].0 as f64);
58+
}
59+
}
60+
61+
ret
62+
}
63+
}
64+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 857. 雇佣 K 名工人的最低成本
2+
`n` 名工人。 给定两个数组 `quality``wage` ,其中,`quality[i]` 表示第 `i` 名工人的工作质量,其最低期望工资为 `wage[i]`
3+
4+
现在我们想雇佣 `k` 名工人组成一个 **工资组**。在雇佣 一组 `k` 名工人时,我们必须按照下述规则向他们支付工资:
5+
1. 对工资组中的每名工人,应当按其工作质量与同组其他工人的工作质量的比例来支付工资。
6+
2. 工资组中的每名工人至少应当得到他们的最低期望工资。
7+
8+
给定整数 `k` ,返回 *组成满足上述条件的付费群体所需的最小金额* 。与实际答案误差相差在 <code>10<sup>-5</sup></code> 以内的答案将被接受。
9+
10+
#### 示例 1:
11+
<pre>
12+
<strong>输入:</strong> quality = [10,20,5], wage = [70,50,30], k = 2
13+
<strong>输出:</strong> 105.00000
14+
<strong>解释:</strong> 我们向 0 号工人支付 70,向 2 号工人支付 35。
15+
</pre>
16+
17+
#### 示例 2:
18+
<pre>
19+
<strong>输入:</strong> quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
20+
<strong>输出:</strong> 30.66667
21+
<strong>解释:</strong> 我们向 0 号工人支付 4,向 2 号和 3 号分别支付 13.33333。
22+
</pre>
23+
24+
#### 提示:
25+
* `n == quality.length == wage.length`
26+
* <code>1 <= k <= n <= 10<sup>4</sup></code>
27+
* <code>1 <= quality[i], wage[i] <= 10<sup>4</sup></code>
28+
29+
## 题解 (Rust)
30+
31+
### 1. 题解
32+
```Rust
33+
use std::collections::BinaryHeap;
34+
35+
impl Solution {
36+
pub fn mincost_to_hire_workers(quality: Vec<i32>, wage: Vec<i32>, k: i32) -> f64 {
37+
let k = k as usize;
38+
let mut workers = quality
39+
.into_iter()
40+
.zip(wage.into_iter())
41+
.collect::<Vec<_>>();
42+
let mut quality_heap = BinaryHeap::new();
43+
let mut quality_sum = 0;
44+
let mut ret = f64::INFINITY;
45+
46+
workers.sort_unstable_by(|(q0, w0), (q1, w1)| (q1 * w0).cmp(&(q0 * w1)));
47+
48+
for i in 0..workers.len() {
49+
if i >= k {
50+
quality_sum -= quality_heap.pop().unwrap();
51+
}
52+
53+
quality_heap.push(workers[i].0);
54+
quality_sum += workers[i].0;
55+
56+
if i >= k - 1 {
57+
ret = ret.min(quality_sum as f64 * workers[i].1 as f64 / workers[i].0 as f64);
58+
}
59+
}
60+
61+
ret
62+
}
63+
}
64+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::collections::BinaryHeap;
2+
3+
impl Solution {
4+
pub fn mincost_to_hire_workers(quality: Vec<i32>, wage: Vec<i32>, k: i32) -> f64 {
5+
let k = k as usize;
6+
let mut workers = quality
7+
.into_iter()
8+
.zip(wage.into_iter())
9+
.collect::<Vec<_>>();
10+
let mut quality_heap = BinaryHeap::new();
11+
let mut quality_sum = 0;
12+
let mut ret = f64::INFINITY;
13+
14+
workers.sort_unstable_by(|(q0, w0), (q1, w1)| (q1 * w0).cmp(&(q0 * w1)));
15+
16+
for i in 0..workers.len() {
17+
if i >= k {
18+
quality_sum -= quality_heap.pop().unwrap();
19+
}
20+
21+
quality_heap.push(workers[i].0);
22+
quality_sum += workers[i].0;
23+
24+
if i >= k - 1 {
25+
ret = ret.min(quality_sum as f64 * workers[i].1 as f64 / workers[i].0 as f64);
26+
}
27+
}
28+
29+
ret
30+
}
31+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@
585585
[852][852l] |[Peak Index in a Mountain Array][852] |![rs]
586586
[853][853l] |[Car Fleet][853] |![rs]
587587
[856][856l] |[Score of Parentheses][856] |![rs]
588+
[857][857l] |[Minimum Cost to Hire K Workers][857] |![rs]
588589
[858][858l] |[Mirror Reflection][858] |![rb]&nbsp;&nbsp;![rs]
589590
[859][859l] |[Buddy Strings][859] |![py]
590591
[860][860l] |[Lemonade Change][860] |![rs]
@@ -2267,6 +2268,7 @@
22672268
[852]:Problemset/0852-Peak%20Index%20in%20a%20Mountain%20Array/README.md#852-peak-index-in-a-mountain-array
22682269
[853]:Problemset/0853-Car%20Fleet/README.md#853-car-fleet
22692270
[856]:Problemset/0856-Score%20of%20Parentheses/README.md#856-score-of-parentheses
2271+
[857]:Problemset/0857-Minimum%20Cost%20to%20Hire%20K%20Workers/README.md#857-minimum-cost-to-hire-k-workers
22702272
[858]:Problemset/0858-Mirror%20Reflection/README.md#858-mirror-reflection
22712273
[859]:Problemset/0859-Buddy%20Strings/README.md#859-buddy-strings
22722274
[860]:Problemset/0860-Lemonade%20Change/README.md#860-lemonade-change
@@ -3943,6 +3945,7 @@
39433945
[852l]:https://leetcode.com/problems/peak-index-in-a-mountain-array/
39443946
[853l]:https://leetcode.com/problems/car-fleet/
39453947
[856l]:https://leetcode.com/problems/score-of-parentheses/
3948+
[857l]:https://leetcode.com/problems/minimum-cost-to-hire-k-workers/
39463949
[858l]:https://leetcode.com/problems/mirror-reflection/
39473950
[859l]:https://leetcode.com/problems/buddy-strings/
39483951
[860l]:https://leetcode.com/problems/lemonade-change/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@
585585
[852][852l] |[山脉数组的峰顶索引][852] |![rs]
586586
[853][853l] |[车队][853] |![rs]
587587
[856][856l] |[括号的分数][856] |![rs]
588+
[857][857l] |[雇佣 K 名工人的最低成本][857] |![rs]
588589
[858][858l] |[镜面反射][858] |![rb]&nbsp;&nbsp;![rs]
589590
[859][859l] |[亲密字符串][859] |![py]
590591
[860][860l] |[柠檬水找零][860] |![rs]
@@ -2267,6 +2268,7 @@
22672268
[852]:Problemset/0852-Peak%20Index%20in%20a%20Mountain%20Array/README_CN.md#852-山脉数组的峰顶索引
22682269
[853]:Problemset/0853-Car%20Fleet/README_CN.md#853-车队
22692270
[856]:Problemset/0856-Score%20of%20Parentheses/README_CN.md#856-括号的分数
2271+
[857]:Problemset/0857-Minimum%20Cost%20to%20Hire%20K%20Workers/README_CN.md#857-雇佣-k-名工人的最低成本
22702272
[858]:Problemset/0858-Mirror%20Reflection/README_CN.md#858-镜面反射
22712273
[859]:Problemset/0859-Buddy%20Strings/README_CN.md#859-亲密字符串
22722274
[860]:Problemset/0860-Lemonade%20Change/README_CN.md#860-柠檬水找零
@@ -3943,6 +3945,7 @@
39433945
[852l]:https://leetcode.cn/problems/peak-index-in-a-mountain-array/
39443946
[853l]:https://leetcode.cn/problems/car-fleet/
39453947
[856l]:https://leetcode.cn/problems/score-of-parentheses/
3948+
[857l]:https://leetcode.cn/problems/minimum-cost-to-hire-k-workers/
39463949
[858l]:https://leetcode.cn/problems/mirror-reflection/
39473950
[859l]:https://leetcode.cn/problems/buddy-strings/
39483951
[860l]:https://leetcode.cn/problems/lemonade-change/

0 commit comments

Comments
 (0)