learn-python/contrib/ds-algorithms/deque.md

108 wiersze
3.3 KiB
Markdown
Czysty Zwykły widok Historia

2024-06-22 15:47:42 +00:00
## Double-Ended Queue (Deque)
A double-ended queue (deque) supports insertion and deletion from both ends, making it a more versatile queue implementation.
### Input-Restricted Deque
In an input-restricted deque, elements can only be inserted from one end while deletions can occur from both ends.
### Output-Restricted Deque
An output-restricted deque allows elements to be deleted from one end only, while insertions can be made from both ends.
## Real Life Examples of Deques
### Task Scheduling
Deques are useful in task scheduling algorithms where tasks can be added to either end and processed accordingly based on priority or other scheduling criteria.
### Sliding Window Problems
Algorithms solving sliding window problems often use deques to efficiently manage and query elements in the current window.
### Implementations in Python
Python provides a built-in `collections.deque` which supports efficient append and pop operations from both ends. It's ideal for scenarios requiring a simple and efficient double-ended queue.
## Operations on a Deque
- **isEmpty**: Checks if the deque is empty.
- **appendLeft**: Adds an element to the left end of the deque.
- **appendRight**: Adds an element to the right end of the deque.
- **popLeft**: Removes and returns the element from the left end of the deque.
- **popRight**: Removes and returns the element from the right end of the deque.
- **peekLeft**: Returns the element from the left end without removing it.
- **peekRight**: Returns the element from the right end without removing it.
- **clear**: Removes all elements from the deque.
## Implementation of Deque in Python
Python's `collections.deque` provides an efficient implementation of a deque.
```python
from collections import deque
# Creating a deque
dq = deque()
# Adding elements to the deque
dq.append(1)
dq.append(2)
dq.append(3)
# Removing elements from the deque
dq.popleft() # Removes and returns 1
dq.pop() # Removes and returns 3
# Peeking elements
print("Left end peek:", dq[0])
print("Right end peek:", dq[-1])
# Displaying elements in the deque
print("Deque:", list(dq))
```
## Example: Sliding Window Maximum
In this example, we'll use a deque to efficiently find the maximum element in sliding windows of a list.
```python
from collections import deque
def sliding_window_maximum(nums, k):
if not nums:
return []
n = len(nums)
result = []
dq = deque()
for i in range(n):
# Remove elements from the deque that are out of the current window
while dq and dq[0] <= i - k:
dq.popleft()
# Remove elements from the deque that are less than the current element
while dq and nums[dq[-1]] <= nums[i]:
dq.pop()
# Add the current element index to the deque
dq.append(i)
# Add the maximum of the current window to the result
if i >= k - 1:
result.append(nums[dq[0]])
return result
# Example usage:
nums = [1, 3, -1, -3, 5, 3, 6, 7]
k = 3
print("Sliding Window Maximum:", sliding_window_maximum(nums, k))
```
## Conclusion
Queues and deques are fundamental data structures that facilitate efficient data processing and
management in various applications. Understanding their principles and implementations is crucial
for developing robust software solutions.