Is it a max-heap?
Warm-up · Write code · about 8 min
Return true when the array values is a valid max-heap: no value is smaller than one of its children. The children of index i are at 2i + 1 and 2i + 2.
An empty array and a single value are valid max-heaps.
Example 1
Input:
[[9,7,8,3,5,6]]
Output:
true
Every parent is at least as large as its children.
Example 2
Input:
[[9,7,8,3,10]]
Output:
false
10 at index 4 is larger than its parent 7 at index 1.
Constraints
- 0 ≤ values.length ≤ 10,000
- Values are integers from -100,000 to 100,000
Learn the idea first: Building a heap in O(n), a visual lesson.
More Building a heap in O(n) problems
- Why is build-heap O(n)? Warm-up
- Heapify Standard
- Count sift steps Standard
- Sift down one index Standard
- Implement build-heap Challenge