LeetcodeMedium- [503. The next larger element II]

A question similar to this one: LeetCodeEasy- [496. Next larger element I]

Given a circular array (the next element of the last element is the first element of the array), output the next larger element of each element. The next larger element of the number x is in the order of traversal of the array, the first number after this number is larger than it, which means that you should search for its next larger number in a loop. If it does not exist, -1 is output.

Example 1:

Input: [1,2,1]
Output: [2, -1,2]
Explanation: The next larger number of the first 1 is 2; the
number 2 cannot find the next larger number; the 
second The next largest number of 1 requires a loop search, and the result is also 2.
Note: The length of the input array will not exceed 10000.

Source: LeetCode
Link: https://leetcode-cn.com/problems/next-greater-element-ii The
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

Idea 1: Inspired by the last question LeetCodeEasy- [496. The next larger element I] , it can be thought that a stack can still be used to find the number. However, you need to know the difference between this question and the previous question: 1. The elements of this question can be repeated: so you ca n’t use a dictionary to store it. You can first initialize an answer array and find one that is directly filled into the answer array; 2. This The question is a circular array: so it is not enough to traverse the question once, and it can be completed by using two traversals.

class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        stack_i = [] # 存放未找到数的下标
        min_num = -0x3f3f3f3f
        i = 0
        length = len(nums)
        ans = [min_num] * length # 存放答案
        cnt = 2 * length
        while cnt > 0:
            cnt -= 1
            while stack_i != [] and nums[i] > nums[stack_i[-1]]:
                ans[stack_i[-1]] = nums[i]
                stack_i.pop()
            if ans[i] == min_num:
                stack_i.append(i)
            i = (i + 1) % length
        for i in range(length):
            if ans[i] == min_num:
                ans[i] = -1
        return ans

Idea 2: The description is as follows, it is recommended to look at the code for a better understanding.

class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        stack_i = [] # 存放未找到数的下标
        length = len(nums)
        ans = [0] * length # 存放答案
        for j in range(2*length - 1, 0, -1): # 必须反向遍历
            i = j % length
            while stack_i != [] and nums[i] >= nums[stack_i[-1]]: # 从后开始弹出不大于当前查找值的数,直到找到或者为空为为止
                stack_i.pop()
            ans[i] = nums[stack_i[-1]] if stack_i != [] else -1
            stack_i.append(i)
        return ans

 

Published 314 original articles · 22 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_39451578/article/details/105176372