← 返回 bytedance 的题目列表Queue and Priority Queue Implementation
类型:online_judge
Implement a Queue and a Priority Queue. These data structures should support the following operations:
Queue: enqueue, dequeue, and get front.
Priority Queue: insert with priority, pull highest priority element, and peek.
Requirements:
enqueue(queue: List[int], element: int) -> None
dequeue(queue: List[int]) -> int
get_front(queue: List[int]) -> int
insert_with_priority(pq: List[Tuple[int, int]], element: int, priority: int) -> None
pull_highest_priority(pq: List[Tuple[int, int]]) -> int
peek(pq: List[Tuple[int, int]]) -> int
Constraints:
The maximum length of the queue and the priority queue is 1000.
The priority of each element is an integer from 1 to 10.
Test Cases:
# Test cases for Queue
enqueue(queue, 1)
enqueue(queue, 2)
assert dequeue(queue) == 1
assert get_front(queue) == 2
# Test cases for Priority Queue
insert_with_priority(pq, 5, 1)
insert_with_priority(pq, 6, 3)
assert pull_highest_priority(pq) == 6
assert peek(pq) == 5
Example
Input
Queue
1
2