2861. 最大合金数

2861. 最大合金数

解法一: 二分查找

go
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
func maxNumberOfAlloys(n int, k int, budget int, composition [][]int, stock []int, cost []int) int {
ans := 0
for _, arr := range composition {
left := 0
right := 200000000
for left < right {
mid := ((right - left + 1) >> 1) + left
if isSuccess(mid, budget, arr, stock, cost) {
left = mid
} else {
right = mid - 1
}
}
ans = max(ans, left)
}
return ans
}

func isSuccess(count, budget int, composition []int, stock []int, cost []int) bool {
total := 0
for i, num := range composition {
total += max(0, count*num-stock[i]) * cost[i]
if total > budget {
return false
}
}
return total <= budget
}

func min(num1, num2 int) int {
if num1 <= num2 {
return num1
}
return num2
}

func max(num1, num2 int) int {
if num1 >= num2 {
return num1
}
return num2
}
作者

wuhunyu

发布于

2024-01-27

更新于

2024-01-27

许可协议