<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Heapify ### Source - lintcode: [(130) Heapify](http://www.lintcode.com/en/problem/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]. What if there is a lot of solutions? Return any of them. ~~~ ### 題解 參考前文提到的 [Heap Sort](http://algorithm.yuanbin.me/zh-cn/basics_sorting/heap_sort.html) 可知此題要實現的只是小根堆的堆化過程,并不要求堆排。 ### C++ ~~~ class Solution { public: /** * @param A: Given an integer array * @return: void */ void heapify(vector<int> &A) { // build min heap for (int i = A.size() / 2; i >= 0; --i) { min_heap(A, i); } } private: void min_heap(vector<int> &nums, int k) { int len = nums.size(); while (k < len) { int min_index = k; // left leaf node search if (k * 2 + 1 < len && nums[k * 2 + 1] < nums[min_index]) { min_index = k * 2 + 1; } // right leaf node search if (k * 2 + 2 < len && nums[k * 2 + 2] < nums[min_index]) { min_index = k * 2 + 2; } if (k == min_index) { break; } // swap with the minimal int temp = nums[k]; nums[k] = nums[min_index]; nums[min_index] = temp; // not only current index k = min_index; } } }; ~~~ ### 源碼分析 堆排的簡化版,最后一步`k = min_index`不能忘,因為增刪節點時需要重新建堆,這樣才能保證到第一個節點時數組已經是二叉堆。 ### 復雜度分析 由于采用的是自底向上的建堆方式,時間復雜度為 (N)(N)(N), 證明待補充... ### Reference - [Heap Sort](http://algorithm.yuanbin.me/zh-cn/basics_sorting/heap_sort.html) - [Heapify 參考程序 Java/C++/Python](http://www.jiuzhang.com/solutions/heapify/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看