-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaximum-number-of-alloys.rs
53 lines (46 loc) · 1.54 KB
/
maximum-number-of-alloys.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
pub fn max_number_of_alloys(
n: i32,
k: i32,
budget: i32,
composition: Vec<Vec<i32>>,
stock: Vec<i32>,
cost: Vec<i32>,
) -> i32 {
(0..k as usize)
.map(|i| {
// 已有的能制造多少
let mut x = (0..n as usize)
.map(|x| stock[x] / composition[i][x])
.min()
.unwrap();
let mut total_cost = 0; // 购买的总消费
loop {
let mut p = 0;
for m in 0..n as usize as usize {
// 表示剩下已有的不够造了,因此需要去买
if stock[m] - x * composition[i][m] <= 0 {
p += composition[i][m] * cost[m];
} else {
if stock[m] - x * composition[i][m] < composition[i][m] {
p += (composition[i][m] - stock[m] + x * composition[i][m])
* cost[m];
}
}
}
if p + total_cost <= budget {
x += 1;
total_cost += p;
} else {
break;
}
}
x
})
.max()
.unwrap()
}
}