1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| func successfulPairs(spells []int, potions []int, success int64) []int { sort.Ints(potions) ans := make([]int, len(spells)) length := len(potions) for i, num := range spells { ans[i] = length - halfSearch(potions, int((success + int64(num) - 1) / int64(num))) } return ans }
func halfSearch(nums []int, target int) int { left := 0 right := len(nums) - 1 for left <= right { mid := ((right - left) >> 1) + left if nums[mid] < target { left = mid + 1 } else { right = mid - 1 } } return left }
|