일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- shiba
- 프로그래머스
- 파이썬
- Generator
- kaggle
- iterator
- Python Implementation
- 715. Range Module
- Class
- Python
- 43. Multiply Strings
- t1
- LeetCode
- data science
- Substring with Concatenation of All Words
- Decorator
- attribute
- concurrency
- 30. Substring with Concatenation of All Words
- 운영체제
- 시바견
- Regular Expression
- Convert Sorted List to Binary Search Tree
- 컴퓨터의 구조
- DWG
- Protocol
- Python Code
- 109. Convert Sorted List to Binary Search Tree
- 밴픽
- 315. Count of Smaller Numbers After Self
Archives
- Today
- Total
Scribbling
Monotonic Stack 본문
Monotonic stack or monotonicly increasing/decreasing stack is a stack which keeps its elements in increasing/decreasing order.
Monotonic stacks are useful when,
- Finding the very previous less element of each element in a vector in O(N) time complexity
- Finding the very next less element of each element in a vector in O(N) time complexity
(1) Code Example
To get the very previous less (or equal) element:
- use decreasing stack + use the top element of the stack
nums = [1, 3, 1, 1, 2]
N = len(nums)
stack = []
res = [-1] * N
# the very previous less (or equal) element:
for i, num in enumerate(nums):
while stack and nums[stack[-1]] > num:
stack.pop()
res[i] = stack[-1] if stack else -1
stack.append(i)
print(res)
To get the very next less or equal element
- use increasing stack + use popped element
nums = [1, 3, 1, 1, 2]
N = len(nums)
stack = []
res = [N] * N
# the very next less or equal element
for i, num in enumerate(nums):
while stack and nums[stack[-1]] >= num:
res[stack.pop()] = i
stack.append(i)
print(res)
the very previous bigger or equal element:
nums = [1, 3, 1, 1, 2]
N = len(nums)
stack = []
res = [-1] * N
# the very previous bigger (or equal) element:
for i, num in enumerate(nums):
while stack and nums[stack[-1]] < num:
stack.pop()
res[i] = stack[-1] if stack else -1
stack.append(i)
print(res)
the very next bigger or equal
nums = [1, 3, 1, 1, 2]
N = len(nums)
stack = []
res = [N] * N
# the very next bigger or equal element
for i, num in enumerate(nums):
while stack and nums[stack[-1]] <= num:
res[stack.pop()] = i
stack.append(i)
print(res)
(2) Sum of Subarray Minimum
https://leetcode.com/problems/sum-of-subarray-minimums/
class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
N = len(arr)
left = [-1] * N
right = [N] * N
stack = []
for i, num in enumerate(arr):
while stack and arr[stack[-1]] > num:
stack.pop()
if stack:
left[i] = stack[-1]
stack.append(i)
stack = []
for i, num in enumerate(arr):
while stack and arr[stack[-1]] > num:
right[stack.pop()] = i
stack.append(i)
ret = 0
for i, num in enumerate(arr):
ret += num * (i - left[i]) * (right[i] - i)
ret %= (10**9+7)
return ret
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
LeetCode: 1996. The Number of Weak Characters in the Game (0) | 2022.10.21 |
---|---|
Range Checking Algorithm (check if there's a point in a given range) (0) | 2022.08.16 |
LeetCode: 839. Similar String Groups (0) | 2022.05.25 |
Shuffle Algorithm: Knuth Shuffle (0) | 2022.01.15 |
AVL Tree, Python Code (0) | 2022.01.07 |