[LeetCode] 1381. Design a Stack With Increment Operation

Updated:

1381. Design a Stack With Increment Operation

μŠ€ν¬λ¦°μƒ· 2020-09-04 μ˜€ν›„ 11 52 52

객체λ₯Ό μ΄μš©ν•΄ μŠ€νƒμ„ κ΅¬ν˜„ν•˜λŠ” 문제.

λ‚˜λ¨Έμ§€λŠ” μŠ€νƒ κΈ°λŠ₯κ³Ό λ˜‘κ°™κ³  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

Categories:

Updated:

Leave a comment