-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.py
27 lines (23 loc) · 888 Bytes
/
solution.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
class CustomStack:
def __init__(self, maxSize: int):
self.maxSize = maxSize
self.stack = []
self.inc = []
def push(self, x: int) -> None:
if len(self.stack) < self.maxSize:
self.stack.append(x)
self.inc.append(0) # Initialize increment for this element
def pop(self) -> int:
if not self.stack:
return -1
idx = len(self.stack) - 1
result = self.stack[idx] + self.inc[idx] # Apply any pending increments
if idx > 0:
self.inc[idx - 1] += self.inc[idx] # Propagate increment to the next element
self.stack.pop()
self.inc.pop()
return result
def increment(self, k: int, val: int) -> None:
limit = min(k, len(self.stack)) - 1
if limit >= 0:
self.inc[limit] += val # Add increment to the bottom k-th element