A next greater element 496. I, 739. Daily temperatures, 503. The next greater element II, 556. The next element greater III

496. Under a greater element I

1 idea : Stack + dictionary storage (forward traversal)
prior to nums2 each element, obtained under a greater element. These answers and put into a hash table, then loop through the array nums1, directly find the corresponding answers.

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        stack = [] 
        hash_table = {}
        for num in nums2:
            while stack and num>stack[-1]: # 寻找当前元素可以作为前面哪个元素的更大元素
                hash_table[stack.pop()] = num
            stack.append(num)
        return [hash_table.get(num,-1) for num in nums1]

Here Insert Picture Description
Ideas 2: reverse traversal

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        stack = []  
        hash_table = {}
        for num in nums2[::-1]:
            while stack and num>=stack[-1]: # 栈中比当前元素小的全部弹出
                stack.pop()
            hash_table[num]= stack[-1] if stack else -1 # 栈不空时存着比当前元素更大的元素
            stack.append(num)
        return [hash_table.get(num,-1) for num in nums1]

Here Insert Picture Description

739. daily temperatures - is actually seeking a greater index difference and current element

The difference is that the stack is stored inside index difference

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

Here Insert Picture Description

503. The next element of a larger II

1 idea : directly to the original array doubling, as long as the answer to the first part of the output.

class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        nums.extend(nums) # 输入翻倍
        n = len(nums)
        ans = [-1]*n
        stack = []
        for i in range(n-1,-1,-1):
            while stack and nums[i]>=stack[-1]:
                stack.pop(-1)
            if stack: ans[i] = stack[-1]
            stack.append(nums[i])
        return ans[:n//2]  # 输出前部分

Here Insert Picture Description
Optimization: through a virtual doubling of input to reduce space, mainly on the index remainder, the equivalent of running it twice on the same data.

class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        n = len(nums)
        ans = [-1]*n
        stack = []
        for index in range(2*n-1,-1,-1):  # 改动
            i = index%n  # 改动 实现循环,相当于在同样的数据上运行了两遍
            while stack and nums[i]>=stack[-1]:
                stack.pop(-1)
            if stack: ans[i] = stack[-1]
            stack.append(nums[i])
        return ans

Here Insert Picture Description

A next greater element 556. III

Reference: 31. The following are arranged a

Published 135 original articles · won praise 5 · Views 7093

Guess you like

Origin blog.csdn.net/qq_27921205/article/details/104361463