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 + , 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"的更多相关文章
- 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 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
随机推荐
- js 实现复制到剪切板 复制按钮兼容各大浏览器
zeroclipboard官网:https://github.com/zeroclipboard/ZeroClipboard 下载压缩包,得到两个“ZeroClipboard.js”和“ZeroCli ...
- C#部分---函数添加基本格式;
格式1:没有参数,没有返回值 (无参无返) 添加函数: /// <summary> /// 累加求和的方法,没有参数,没有返回值 /// </summary> public v ...
- JavaWeb学习记录(二十)——Model1模式(javaBean+jsp)实现简单计算器案例
¨JSP技术提供了三个关于JavaBean组件的动作元素,即JSP标签,它们分别为: ¨<jsp:useBean>标签:用于在JSP页面中查找或实例化一个JavaBean组件. ¨< ...
- Codeforces Round #145 (Div. 2, ACM-ICPC Rules)
A. Lefthanders and Righthanders \(i\)与\(i+\frac n2\)匹配,根据左右手调整位置. B. Reading 排序,取前\(k\)个. C. Weather ...
- Codeforces Round #131 (Div. 2)
A. System of Equations \(a\)的范围在\(\sqrt n\)内,所以暴力枚举即可. B. Hometask 需要被2.5整除,所以末位必然为0,如果0没有出现,则直接返回-1 ...
- 黑马程序员——JAVA基础之多线程的线程间通讯等
------- android培训.java培训.期待与您交流! ---------- 线程间通讯: 其实就是多个线程在操作同一个资源,但是动作不同. wait(); 在其他线程调用此对象的notif ...
- 黑马程序员——JAVA基础之数组
------- android培训.java培训.期待与您交流! ---------- 数组: 数组的定义: 数组是相同类型数据的集合, 描述的是相同类型的若干个数据按照一定的先后顺序排列组合而成,其 ...
- Windows下IntelliJ IDEA中运行Spark Standalone
ZHUAN http://www.cnblogs.com/one--way/archive/2016/08/29/5818989.html http://www.cnblogs.com/one--wa ...
- C#中的static、readonly与const的比较
C#中有两种常量类型,分别为readonly(运行时常量)与const(编译时常量),本文将就这两种类型的不同特性进行比较并说明各自的适用场景. 工作原理 readonly为运行时常量,程序运 ...
- Js中的window.parent ,window.top,window.self ,window.openner详解
在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口, opener是用open方法 ...