2300. 咒语和药水的成功对数

2300. 咒语和药水的成功对数

解法一: 二分查找

go
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
}
作者

wuhunyu

发布于

2023-11-10

更新于

2023-11-10

许可协议