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 }
|