3296. 移山所需的最少秒数
题目链接 | 3296. 移山所需的最少秒数 |
---|---|
思路 | 问题求解中的值存在“单调性”,可以二分查找 |
题解链接 | 1. 两种方法:最小堆模拟/二分答案(Python/Java/C++/Go) 2. Wiki |
关键点 | 1. 确定可二分 2. 根据时间\(t\),得出下降的高度\(h\) 3. 确定二分范围 |
时间复杂度 | \(O(n\log U)\) |
空间复杂度 | \(O(1)\) |
代码实现:
class Solution:
def minNumberOfSeconds(self, mountainHeight: int, workerTimes: List[int]) -> int:
def check(m):
remain = mountainHeight
for t in workerTimes:
remain -= int((sqrt(m // t * 8 + 1) - 1) / 2)
if remain <= 0:
return True
return False
maxv = max(workerTimes)
h = (mountainHeight - 1) // len(workerTimes) + 1
right = maxv * h * (h + 1) // 2
return bisect_left(
range(right), True, 1, key=check
)