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

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 * + , ri = i * + ;
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() / ; i >= ; i --)
help(A, i);
}
};

LintCode "Heapify"的更多相关文章

  1. Lintcode: Heapify && Summary: Heap

    Given an integer array, heapify it into a min-heap array. For a heap array A, A[0] is the root of he ...

  2. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  3. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  4. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  5. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  6. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  7. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  8. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  9. Lintcode 175. 翻转二叉树

    -------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...

随机推荐

  1. 多层CCLayer的touch冲突解决

    一般通过layer. setTouchPriority()方法来设置 touch优先级,数值越小,优先级越高,但有时多人开发过程中,多层layer叠在一起,无法通过setTouchPrority()来 ...

  2. Spring 框架获取 datasource对象的方法

    1,使用org.springframework.jdbc.datasource.DriverManagerDataSource  2.使用org.apache.commons.dbcp.BasicDa ...

  3. DB2 函数大全

    DB2函数大全 函数名 函数解释 函数举例 AVG() 返回一组数值的平均值. SELECTAVG(SALARY)FROMBSEMPMS; CORR(),CORRELATION() 返回一对数值的关系 ...

  4. JAVA 23种设计模式(转)

    1.FACTORY?追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说“来四个鸡翅”就行了.麦当劳和肯德基就是生产鸡翅 ...

  5. mysql 关联删除

    参考网址:http://www.111cn.net/database/mysql/51146.htm 原网页广告太多,自己抄了下. 1.delete from t1 where 条件2.delete ...

  6. 论文笔记之:Fully-Convolutional Siamese Networks for Object Tracking

    gansh Fully-Convolutional Siamese Network for Object Tracking 摘要:任意目标的跟踪问题通常是根据一个物体的外观来构建表观模型.虽然也取得了 ...

  7. 浅谈 Active Learning

    1. Active Query Driven by Uncertainty and Diversity for Incremental Multi-Label Learning The key tas ...

  8. acess the C\c++ from the Java

    https://en.wikipedia.org/wiki/Java_Native_Interface http://docs.oracle.com/javase/7/docs/technotes/g ...

  9. <button>会自动提交表单吗?

    点击button以后,表单先由ajax提交,然后无论后台返回什么结果,页面都会跳转到表单action属性指定的路劲,也就是login.html使用的是html.jquery.javascript,后台 ...

  10. HTML兼容性设置

    今天碰到了兼容性问题,页面显示空白,打开调试界面,显示信息 “Compatibility View because 'Display intranet sites in Compatibility V ...