【python刷题】单调栈

模板:找到每个元素后面第一个比它大的数,不存在时值为-1

def template():
    stack = []
    nums = [2,1,2,4,3]
    res = [-1 for _ in range(len(nums))]
    for i in range(len(nums)-1,-1,-1):
        while stack and nums[i] >= stack[-1]:
            stack.pop()
        res[i] = stack[-1] if stack else -1
        stack.append(nums[i])
    return res
res = template()
print(res)

[4,2,4,-1,-1]

496. 下一个更大元素 I

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        stack = []
        dic = {} 
        for num in nums2:
            while stack and num > stack[-1]:
                dic[stack.pop()] = num
            stack.append(num)
        return [dic.get(i, -1 ) for i in nums1]

739. 每日温度

class Solution:
    def dailyTemperatures(self, T: List[int]) -> List[int]:
        stack = []
        res = [0 for _ in range(len(T))]
        for i in range(len(T)-1,-1,-1):
            while stack and T[i] >= T[stack[-1]]:
                stack.pop()
            res[i] = stack[-1] - i if stack else 0
            stack.append(i)
        return res

503. 下一个更大元素 II

方法一:套用模板,额外的是对于最后一个元素,我们要将数组长度翻倍

class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        length = len(nums)
        nums = nums * 2
        stack = []
        res = [-1 for _ in range(len(nums))]
        for i in range(len(nums)-1,-1,-1):
            while stack and nums[i] >= stack[-1]:
                stack.pop()
            res[i] =  stack[-1] if stack else -1
            stack.append(nums[i])
        print(res)
        return res[:length]    

方法二:利用循环数组技巧

以下代码让我们可以循环打印数组的值

arr = [1,2,3,4,5]
n = len(arr)
ind = 0
while True:
    print(arr[ind % n])
    ind += 1

根据上述技巧,我们可以写出以下代码:

class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        n = len(nums)
        stack = []
        res = [-1 for _ in range(len(nums))]
        for i in range(2*n-1,-1,-1):
            while stack and nums[i % n] >= stack[-1]:
                stack.pop()
            res[i % n] =  stack[-1] if stack else -1
            stack.append(nums[i % n])
        return res   

执行步骤:

# n = 3
# 2*3-1 = 5
# i = 5,4,3,2,1,0
# i % n = 2,1,0,2,1,0
i = 5
res = [-1,-1,-1]
stack.append(nums[2]) = [2]
i = 4
while [2] and 4>2:
    执行
stack = []
res = [-1,-1,-1]
stack.append(nums[1]) = [4]
i = 3
while [4] and 3>4:
    不执行
res = [4,-1,-1]
stack.append(nums[0]) = [4,3]
i = 2
while [4,3] and 2>3:
    不执行
res[2] =[4,-1,3]
stack.append(nums[2]) = [4,3,2]
i = 1
while [4,3,2] and 4>2:
    执行
stack = []
res = [4,-1,3]
stack.append(nums[1]) = [4]
i = 0
while [4] and 3>4:
    不执行
res[nums[0]] = [4,-1,3]
stack.append(nums[0]) = [4,3]
原文地址:https://www.cnblogs.com/xiximayou/p/14345979.html