-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum Number of Robots Within Budget
41 lines (29 loc) · 1.12 KB
/
Maximum Number of Robots Within Budget
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
// 2398. Maximum Number of Robots Within Budget
# Sliding Window Approach
class Solution:
def maximumRobots(self, chargeTimes: List[int], runningCosts: List[int], budget: int) -> int:
n = len(chargeTimes)
maxHeap = []
freq = defaultdict(int)
bag = set()
i = 0
maxRobots = 0
summ = 0
for j in range(n):
summ += runningCosts[j]
freq[chargeTimes[j]] += 1
if chargeTimes[j] not in bag:
bag.add(chargeTimes[j])
heappush(maxHeap, -chargeTimes[j])
while len(maxHeap) > 0 and (-maxHeap[0] + (j - i + 1) * summ) > budget:
e = runningCosts[i]
c = chargeTimes[i]
summ -= e
if c == -maxHeap[0]:
freq[c] -= 1
if freq[c] == 0:
heappop(maxHeap)
bag.remove(c)
i += 1
maxRobots = max(maxRobots, j - i + 1)
return maxRobots