返回
深入浅出剖析数组模拟的栈和队列,探究单调栈和队列的奥秘
闲谈
2023-09-30 15:18:46
数组模拟栈和队列:从基础到实现
栈:后进先出
栈(Stack)是一种遵循后进先出(LIFO)原则的数据结构,就像一摞盘子,新盘子总是放在最上面,而要拿盘子只能从最上面拿。
数组模拟栈
使用数组模拟栈非常简单,只需要将元素顺序存储在数组中,并使用两个指针来跟踪栈顶和栈底的位置。当压入元素时,将元素放在栈顶并更新栈顶指针;当弹出元素时,从栈顶取出元素并更新栈顶指针。
队列:先进先出
队列(Queue)是一种遵循先进先出(FIFO)原则的数据结构,就像排队等候一样,先到的人先得到服务。
数组模拟队列
使用数组模拟队列也比较简单,只需要将元素顺序存储在数组中,并使用两个指针来跟踪队首和队尾的位置。当入队时,将元素放在队尾并更新队尾指针;当出队时,从队首取出元素并更新队首指针。
单调栈和队列:发现隐藏的秩序
单调栈:有序元素的容器
单调栈(Monotonic Stack)是一种特殊的栈,其中元素具有单调性,即元素要么从栈底到栈顶单调递增,要么从栈底到栈顶单调递减。
单调栈的应用
单调栈在算法中有很多应用,例如:
- 寻找数组中的最大值和最小值
- 寻找数组中的最长递增子序列
- 计算直方图中的最大面积
单调队列:有序元素的缓冲区
单调队列(Monotonic Queue)是一种特殊的队列,其中元素也具有单调性,即元素要么从队首到队尾单调递增,要么从队首到队尾单调递减。
单调队列的应用
单调队列在算法中也有很多应用,例如:
- 计算滑动窗口的最大值和最小值
- 寻找数组中的最长递增子序列
- 计算直方图中的最大面积
实例探究:揭示数据结构的魅力
单调栈应用实例:寻找数组中的最大值和最小值
def max_min_in_array(arr):
"""
Finds the maximum and minimum values in an array using a monotonic stack.
Args:
arr: The input array.
Returns:
A tuple containing the maximum and minimum values in the array.
"""
# Create a stack to store the indices of the elements in the array.
stack = []
# Iterate over the array.
for i, el in enumerate(arr):
# While the stack is not empty and the element at the top of the stack is less
# than the current element, pop the element at the top of the stack.
while stack and arr[stack[-1]] < el:
stack.pop()
# Push the index of the current element onto the stack.
stack.append(i)
# The maximum value in the array is the element at the top of the stack.
max_value = arr[stack[-1]]
# Pop all elements from the stack until it is empty.
while stack:
stack.pop()
# Iterate over the array again.
for i, el in enumerate(arr):
# While the stack is not empty and the element at the top of the stack is greater
# than the current element, pop the element at the top of the stack.
while stack and arr[stack[-1]] > el:
stack.pop()
# Push the index of the current element onto the stack.
stack.append(i)
# The minimum value in the array is the element at the top of the stack.
min_value = arr[stack[-1]]
# Return the maximum and minimum values.
return max_value, min_value
单调队列应用实例:计算滑动窗口的最大值和最小值
def max_min_in_sliding_window(arr, window_size):
"""
Finds the maximum and minimum values in a sliding window of a given size in an array.
Args:
arr: The input array.
window_size: The size of the sliding window.
Returns:
A list of tuples containing the maximum and minimum values in each sliding window.
"""
# Create a deque to store the indices of the elements in the sliding window.
window = deque()
# Create a list to store the maximum and minimum values in each sliding window.
max_min_values = []
# Iterate over the array.
for i, el in enumerate(arr):
# While the deque is not empty and the element at the front of the deque is
# less than the current element, pop the element at the front of the deque.
while window and arr[window[0]] < el:
window.popleft()
# While the deque is not empty and the element at the back of the deque is
# greater than the current element, pop the element at the back of the deque.
while window and arr[window[-1]] > el:
window.pop()
# Push the index of the current element onto the deque.
window.append(i)
# If the size of the deque is equal to the size of the sliding window, then
# the current window is complete.
if len(window) == window_size:
# Add the maximum and minimum values in the current window to the list.
max_min_values.append((arr[window[0]], arr[window[-1]]))
# Pop the index of the element at the front of the deque.
window.popleft()
# Return the list of maximum and minimum values.
return max_min_values
结语
数组模拟的栈和队列是两种非常重要的数据结构,它们在算法中有着广泛的应用。单调栈和队列是栈和队列的特殊变种,它们具有单调性,这使得它们在某些问题中具有很强的优势。
希望这篇文章能帮助您理解数组模拟的栈和队列,以及单调栈和队列的奥秘。如果您有任何问题或建议,请随时留言,我会尽力解答。