Skip to content

Monotonic Stack #21

@caipengbo

Description

@caipengbo

单调栈框架
以单调递增栈为例:
Next Less Element
Previous Less Element

for (int i = 0; i < n; i++) {
    right[i] = n-i;  // 某些元素不能进到pop循环,所以赋初值
    while (!stack.empty() && A[stack.peek()] > A[i]) {
        int peek = stack.pop();
        right[peek] = i - peek;  //  pop时操作  Next Less Element
    }
    // 栈为空时需要特殊赋值
    left[i] = (stack.empty() ? i+1 : i - stack.peek());   // push时操作,Previous Less Element
    stack.push(i);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions