package
0.0.0-20241220224003-b7cf03a90b2b
Repository: https://github.com/szhou12/leetcode-go.git
Documentation: pkg.go.dev
# README
2861. Maximum Number of Alloys
Solution idea
Binary Search 猜答案
- 题目要求只能使用一台机器制造alloys,容易想到遍历每种机器,分别找出可以造出的alloys上限,再取最大值。每台机器可以造出的上限可以通过Binary Search猜答案来快速查找,收敛条件是猜的alloys数不超过budget。
- 骨架:
loop k machines:
binary search max # of alloys machine i can produce without exceeding buedget
- 注意!Binary Search的最高上界不能定得太高 (e.g.
right = math.MaxInt
orright = math.MaxInt/2
),都会在计算cost时发生溢出。网上的参考答案可以给到right = int(1e9)
(int(1e8)
不行),猜测可能与constraint0 <= stock[i] <= 10^8
有关。
Time complexity = $O(k\cdot \log(10^9) \cdot n)$