Heapify


Medium Heapify
Given an integer array, heapify it into a min-heap array.
For a heap array A, A[0] is the root of heap, and for each A[i], A[i * 2 + 1] is the left child of A[i] and A[i * 2 + 2] is the right child of A[i].


Example

Given [3,2,1,4,5], return [1,2,3,4,5] or any legal heap array.

Challenge

O(n) time complexity

Clarification

What is heap?

  • Heap is a data structure, which usually have three methods: push, pop and top. where "push" add a new element the heap, "pop" delete the minimum/maximum element in the heap, "top" return the minimum/maximum element.

What is heapify?
  • Convert an unordered integer array into a heap array. If it is min-heap, for each element A[i], we will get A[i * 2 + 1] >= A[i] and A[i * 2 + 2] >= A[i].

想起了算法期中考试最后一题。。。刷题leetcode很多遍了,感觉leetcode有点进阶,这种基础的算法结构题都不收入。 这个题的模式是我的一个盲点。mark 一下

过俩天补上这题iterative的做法

public class Solution {
    /**
     * @param A: Given an integer array
     * @return: void
     */
    public void heapify(int[] A) {
        // write your code here
        for(int i = A.length/2; i>=0; i--){
            helper(A,i);
        }
    }
    
    public void helper(int[] A, int k){
        if(k >= A.length)
            return;
        int left = 2*k+1 >= A.length? Integer.MAX_VALUE:A[2*k+1];
        int right = 2*k+2 >= A.length? Integer.MAX_VALUE:A[2*k+2];
        
        if(left < right && left < A[k]){
            A[2*k+1] = A[k];
            A[k] = left;
            helper(A, 2*k+1);
        }else if(right < A[k]){
            A[2*k+2] = A[k];
            A[k] = right;
            helper(A, 2*k+2);
        }
    }
}

### 使用 `heapq.heapify` 的方法及其实现 在 Python 中,`heapq` 是一个用于处理堆数据结构的标准库模块。其中的 `heapify` 函数可以将普通的列表转换成最小堆(min-heap)。以下是关于该函数的具体说明: #### 什么是 `heapq.heapify` `heapq.heapify` 将任意列表原地调整为堆结构[^1]。具体来说,它会重新排列列表中的元素,使得父节点始终小于其子节点(对于最小堆而言),从而满足二叉堆性质。 调用方式如下: ```python import heapq data = [5, 7, 9, 1, 3] heapq.heapify(data) print(data) # 输出: [1, 3, 9, 7, 5] ``` 上述代码展示了如何通过 `heapq.heapify` 方法将原始列表 `[5, 7, 9, 1, 3]` 转换成最小堆形式。 #### 实现原理 `heapq.heapify` 的核心思想基于自底向上的堆化过程。算法从最后一个非叶子节点开始向前遍历,并对每个节点执行下沉操作(sift-down),直到整个数组被调整为有效的堆结构[^4]。 下面是手动模拟 `heapq.heapify` 功能的一个简单版本: ```python def heapify(arr): n = len(arr) # 自底向上构建堆 for i in range(n // 2 - 1, -1, -1): sift_down(arr, i, n) def sift_down(heap, startpos, endpos): root = startpos while (child := 2 * root + 1) < endpos: child_to_swap = child if child + 1 < endpos and heap[child] > heap[child + 1]: child_to_swap += 1 if heap[root] <= heap[child_to_swap]: break heap[root], heap[child_to_swap] = heap[child_to_swap], heap[root] root = child_to_swap # 测试 arr = [5, 7, 9, 1, 3] heapify(arr) print(arr) # 输出: [1, 3, 9, 7, 5] ``` 此代码片段定义了一个名为 `heapify` 的辅助函数以及内部使用的 `sift_down` 下沉逻辑来完成堆化的功能。 #### 应用场景 `heapq.heapify` 常见的应用包括但不限于以下几种情况: - 构建优先级队列; - 执行高效的选择排序变体——堆排序; - 寻找前 N 大或者前 N 小的数据项。 例如,在寻找最大的三个数时可利用 `heapq.nlargest` 方法,这实际上依赖于先创建一个小顶堆再逐步提取最大值的过程。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值