1381. Design a Stack With Increment Operation (Medium) - TengnanYao/daily_leetcode GitHub Wiki
class CustomStack(object):
def __init__(self, maxSize):
"""
:type maxSize: int
"""
self.s = []
self.n = maxSize
self.inc = []
def push(self, x):
"""
:type x: int
:rtype: None
"""
if len(self.s) < self.n:
self.s.append(x)
self.inc.append(0)
def pop(self):
"""
:rtype: int
"""
if not self.s:
return -1
if len(self.s) > 1:
self.inc[-2] += self.inc[-1]
return self.s.pop() + self.inc.pop()
def increment(self, k, val):
"""
:type k: int
:type val: int
:rtype: None
"""
if self.s:
if k < len(self.s):
self.inc[k - 1] += val
else:
self.inc[-1] += val
# class CustomStack(object):
# def __init__(self, maxSize):
# """
# :type maxSize: int
# """
# self.s = []
# self.n = maxSize
# def push(self, x):
# """
# :type x: int
# :rtype: None
# """
# if len(self.s) < self.n:
# self.s.append(x)
# def pop(self):
# """
# :rtype: int
# """
# return self.s.pop() if self.s else -1
# def increment(self, k, val):
# """
# :type k: int
# :type val: int
# :rtype: None
# """
# n = min(k, len(self.s))
# for i in range(n):
# self.s[i] += val
# Your CustomStack object will be instantiated and called as such:
# obj = CustomStack(maxSize)
# obj.push(x)
# param_2 = obj.pop()
# obj.increment(k,val)