Priority Queue Min
Given an array representing a priority queue (min-heap), return the minimum element without removing it. In a min-heap priority queue, the smallest element is always at the root (index 0).
For example, the minimum of [1, 3, 5, 7, 9] is 1. The minimum of [-5, 0, 10] is -5. If the array is empty, return 0. The operation does not modify the priority queue.
Peeking at the minimum element is a fundamental priority queue operation. It allows you to inspect the highest-priority (smallest) element without actually removing it. This is useful when you need to conditionally process the next item or when multiple queues need to be compared by their minimum values.
In a min-heap, the root at index 0 is always the smallest element. This is guaranteed by the heap invariant: every parent node is smaller than or equal to its children. Therefore, peeking at the minimum is an O(1) operation — just return the first element of the array.
Edge cases include an empty priority queue (return 0), a single-element queue (return that element), and a queue with all equal values (return that value). The heap structure ensures the minimum is always at the root regardless of how many elements have been inserted or extracted.
Example Input & Output
Min is 5
Single element
Min is 1
Empty
Negative min
Algorithm Flow
Solution Approach
Return the first element of the array, which is the root of the min-heap.
In a min-heap array, the smallest element is always at index 0 (the root). Return it if the array is not empty. If the array is empty, return 0 as a sentinel value. This works because the heap invariant guarantees that every parent is smaller than its children, so the minimum bubbles up to the root.
Time complexity is O(1), space complexity is O(1).
Best Answers
class Solution {
public int solution(int[] nums) {
if(nums.length==0)return -1;int m=nums[0];for(int n:nums){if(n<m)m=n;}return m;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
