[LeetCode] 1381. Design a Stack With Increment Operation
Updated:
1381. Design a Stack With Increment Operation
κ°μ²΄λ₯Ό μ΄μ©ν΄ μ€νμ ꡬννλ λ¬Έμ .
λλ¨Έμ§λ μ€ν κΈ°λ₯κ³Ό λκ°κ³ increment κΈ°λ₯λ§ κ΅¬ν νλ©΄ λλ€.
k κ° maxsize λ³΄λ€ ν¬κ² μ£Όμ΄μ§ μ λ μκ³ k κ° νμ¬ μ€νμ μλ λ°μ΄ν° λ³΄λ€ λ λ§μ΄ μ€ μλ μμ΄ μμΈμ²λ¦¬λ₯Ό ν΄μ£Όμ΄μΌ νλ€.
class CustomStack:
def __init__(self, maxSize: int):
self.my_maxsize = maxSize
self.my_stack = []
def push(self, x: int) -> None:
if len(self.my_stack) < self.my_maxsize:
self.my_stack.append(x)
def pop(self) -> int:
if self.my_stack:
return self.my_stack.pop()
else:
return -1
def increment(self, k: int, val: int) -> None:
idx = 0
while idx < min(k, self.my_maxsize):
try:
self.my_stack[idx] += val
except IndexError:
break
idx += 1
Leave a comment