zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    };
    View Code

    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);
        }
    };
  • 相关阅读:
    luogu P2639 [USACO09OCT]Bessie的体重问题Bessie's We…
    1001. 害死人不偿命的(3n+1)猜想 (15)
    1003. 我要通过!(20)
    1002. 写出这个数 (20)
    《C语言程序设计(第四版)》阅读心得(一)
    1006. 换个格式输出整数 (15)
    背包问题之多重背包
    背包问题之完全背包
    背包问题之0-1背包
    动态规划例题
  • 原文地址:https://www.cnblogs.com/tonix/p/4862781.html
Copyright © 2011-2022 走看看