3296. 移山所需的最少秒数

题目链接 3296. 移山所需的最少秒数
思路 问题求解中的值存在“单调性”,可以二分查找
题解链接 1. 两种方法:最小堆模拟/二分答案(Python/Java/C++/Go) 2. Wiki
关键点 1. 确定可二分 2. 根据时间t,得出下降的高度h 3. 确定二分范围
时间复杂度 O(nlogU)
空间复杂度 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
)
posted @   WrRan  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示