-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
maximum-number-of-robots-within-budget.py
58 lines (51 loc) · 1.68 KB
/
maximum-number-of-robots-within-budget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Time: O(n)
# Space: O(n)
import collections
# sliding window, two pointers, mono deque
class Solution(object):
def maximumRobots(self, chargeTimes, runningCosts, budget):
"""
:type chargeTimes: List[int]
:type runningCosts: List[int]
:type budget: int
:rtype: int
"""
result = left = curr = 0
dq = collections.deque()
for right in xrange(len(chargeTimes)):
while dq and chargeTimes[dq[-1]] <= chargeTimes[right]:
dq.pop()
dq.append(right)
curr += runningCosts[right]
if chargeTimes[dq[0]]+(right-left+1)*curr > budget:
if dq[0] == left:
dq.popleft()
curr -= runningCosts[left]
left += 1
return right-left+1
# Time: O(n)
# Space: O(n)
import collections
# sliding window, two pointers, mono deque
class Solution2(object):
def maximumRobots(self, chargeTimes, runningCosts, budget):
"""
:type chargeTimes: List[int]
:type runningCosts: List[int]
:type budget: int
:rtype: int
"""
result = left = curr = 0
dq = collections.deque()
for right in xrange(len(chargeTimes)):
while dq and chargeTimes[dq[-1]] <= chargeTimes[right]:
dq.pop()
dq.append(right)
curr += runningCosts[right]
while dq and chargeTimes[dq[0]]+(right-left+1)*curr > budget:
if dq[0] == left:
dq.popleft()
curr -= runningCosts[left]
left += 1
result = max(result, right-left+1)
return result