Interview questions must be brushed: monotonous stack python template routine (with detailed explanation of usage examples)

Monotonic stack problem:

To know what kind of problems the monotonic stack is suitable for, you first need to know the role of the monotonic stack. The monotonic stack is divided into a monotonic increasing stack and a monotonic decreasing stack. By using the monotonic stack, we can access the next element larger (smaller) than him (or we can).

That is to say, in a queue or an array, we usually use a monotonic stack when we need to compare the size relationship of the elements before and after to solve the problem. Below we briefly introduce the monotonic stack reduction and monotonic increase stack problem to further explain the process of using the monotonic stack to deal with the problem.

When to use a monotonic stack?
It is usually a one-dimensional array. It is necessary to find the first element on the right (left) of any element that is larger (smaller) than itself, and requires O(n) time complexity

Monotonic stack principle:

Monotonically increasing stack: increasing from the bottom of the stack to the top of the stack, the top of the stack is large
Monotonically decreasing stack: decreasing from the bottom of the stack to the top of the stack, the top of the stack is small

Python template routines:

1) : Find the first position larger than itself for the current item to the right-maintain a monotonically decreasing stack from right to left.

def nextGreaterElement_01(nums: list):
    length = len(nums)
    res, stack = [-1] * length, []

    for i in range(length - 1, -1, -1):
        while stack and stack[-1] <= nums[i]:
            stack.pop()
        if stack:
            res[i] = stack[-1]
        stack.append(nums[i])

    return res

Or the current item to the right to find the first position greater than itself-maintain a monotonically decreasing stack from left to right.

def nextGreaterElement_011(nums: list):
    length = len(nums)
    res, stack = [-1] * length, []

    for i in range(length):
        while stack and nums[stack[-1]] < nums[i]:
            idx = stack.pop()
            res[idx] = nums[i]
        stack.append(i)

    return res

2) : Find the first position smaller than itself for the current item to the right-maintain a monotonically increasing stack from right to left

def nextGreaterElement_02(nums: list):
    length = len(nums)
    res, stack = [-1] * length, []

    for i in range(length - 1, -1, -1):
        while stack and stack[-1] >= nums[i]:
            stack.pop()
        if stack:
            res[i] = stack[-1]
        stack.append(nums[i])

    return res

3) : Find the first position larger than itself for the current item to the left-maintain a monotonically decreasing stack from left to right

def nextGreaterElement_03(nums: list):
    length = len(nums)
    res, stack = [-1] * length, []

    for i in range(length):
        while stack and stack[-1] <= nums[i]:
            stack.pop()
        if stack:
            res[i] = stack[-1]
        stack.append(nums[i])

    return res

4) : Find the first position smaller than itself for the current item to the left-maintain a monotonically increasing stack from left to right

def nextGreaterElement_04(nums: list):
    length = len(nums)
    res, stack = [-1] * length, []

    for i in range(length):
        while stack and stack[-1] >= nums[i]:
            stack.pop()
        if stack:
            res[i] = stack[-1]
        stack.append(nums[i])

    return res

Reference source: https://leetcode-cn.com/problems/next-greater-element-i/solution/dan-diao-zhan-zong-jie-by-wu-xian-sen-2/

Guess you like

Origin blog.csdn.net/weixin_44414948/article/details/114385843