Is this array a min-heap?
Warm-up · Write code · about 8 min
Given an array values, return true if it is a valid min-heap: every value is at least as large as its parent. The parent of index i is (i - 1) / 2 rounded down.
An empty array and equal values are valid.
Example 1
Input:
[[1,3,2,6,4]]
Output:
true
Example 2
Input:
[[3,1,2]]
Output:
false
1 is smaller than its parent 3.
Constraints
- 0 ≤ values.length ≤ 10,000
- -1,000,000 ≤ values[i] ≤ 1,000,000
Learn the idea first: Binary heaps: array layout, sift up, sift down, a visual lesson.
More Binary heaps: array layout, sift up, sift down problems
- Parent and child indices Warm-up
- Which array is a min-heap? Warm-up
- Insert into a heap Warm-up
- Extract the minimum Standard
- Implement push and pop Standard
- Spot the bug: sift down Standard
- Implement a max-heap with decrease-key Challenge