LintCode "Heapify"

My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only failed with TLE with the last test case. The problem is, partial sort cannot guaratee O(n) every time.

class Solution
{
    void kth(vector<int> &A, int s, int e, int k) // all zero based
    {
        if(s >= e) return;

        //    Partition
        int i = e, j = e;
        {
            std::random_device rd;
            std::mt19937 gen(rd());
            std::uniform_int_distribution<> dis(s, e);

            int pi = dis(gen);
            int pivot = A[pi];
            swap(A[pi], A[s]);

            while(j > s)
            {
                if(A[j] >= pivot)
                {
                    swap(A[i], A[j]);
                    i --; j = i;
                }
                else
                {
                    j --;
                }
            }
            swap(A[i], A[s]);
        }

        //    Recursion
        if(i < k)
        {
            kth(A, i + 1, e, k);
        }
        else if(i > k)
        {
            kth(A, s, i - 1, k);
        }
    }
public:
    /**
     * @param A: Given an integer array
     * @return: void
     */
    void heapify(vector<int> &A)
    {
        size_t n = A.size();
        int s = 0, e = n - 1;
        while (s < e)
        {
            int cnt = e - s + 1;
            int h = ceil(log2(cnt));
            int k = (pow(2, h) - 1)/2;
            kth(A, s, e, k - 1);
            e = k - 1;
        }
    }
};

A smarter way is as below. Its strategy is "per-node maintanence".

class Solution {
    void help(vector<int> &A, int i)
    {
        int n = A.size();
        int li = i * 2 + 1, ri = i * 2 + 2;
        int left = li < n ? A[li] : INT_MAX;
        int right= ri < n ? A[ri] : INT_MAX;

        if(left < right  && left < A[i])
        {
            swap(A[li], A[i]);
            help(A, li);
        }
        else if(right < left && right < A[i])
        {
            swap(A[ri], A[i]);
            help(A, ri);
        }
    }
public:
    /**
     * @param A: Given an integer array
     * @return: void
     */
    void heapify(vector<int> &A) {
        for(int i = A.size() / 2; i >= 0; i --)
            help(A, i);
    }
};
时间: 2024-08-04 07:14:32

LintCode "Heapify"的相关文章

Lintcode: Heapify &amp;&amp; Summary: Heap

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,

[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param

lintcode.44 最小子数组

最小子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Apple Epic Systems TinyCo Yelp Hedvig Zenefits Uber Snapchat Yahoo Microsoft Bloomberg Facebook Google

lintcode 66.67.68 二叉树遍历(前序、中序、后序)

AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The r

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1

Lintcode 469. 等价二叉树

----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */

Lintcode 75.寻找峰值

--------------------------------------- 按照给定的峰值定义,峰值的左半部分一定是递增的,所以只要找到不递增的即可. AC代码: class Solution { /** * @param A: An integers array. * @return: return any of peek positions. */ public int findPeak(int[] A) { for(int i=1;i<A.length;i++){ if(A[i]>=

Lintcode 9.Fizz Buzz 问题

------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++

Lintcode 97.二叉树的最大深度

--------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class S