6143. 预算内的最多机器人数目
单调队列维护 滑动窗口中chargeTimes的最大值,二分枚举区间大小。
import collections
def work(cs, rs, k):
dq = collections.deque()
n = len(cs)
cursum = 0
for i in range(0, k):
cursum += rs[i]
while len(dq) > 0 and cs[dq[-1]] <= cs[i]:
dq.pop()
dq.append(i)
res = k * cursum + cs[dq[0]]
for i in range(k, n):
if dq[0] <= i - k:
dq.popleft()
while len(dq) > 0 and cs[dq[-1]] <= cs[i]:
dq.pop()
dq.append(i)
cursum -= rs[i-k]
cursum += rs[i]
res = min(res, k * cursum + cs[dq[0]])
return res
class Solution(object):
def maximumRobots(self, chargeTimes, runningCosts, budget):
"""
:type chargeTimes: List[int]
:type runningCosts: List[int]
:type budget: int
:rtype: int
"""
n = len(chargeTimes)
left = 0
right = n
while left < right:
mid = left + right + 1 >> 1
res = work(chargeTimes, runningCosts, mid)
if res<= budget:
left = mid
else :
right = mid - 1
return left
网友评论