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 ...
随机推荐
- c++11编码规范 NULL还是nullptr
0和nullptr/NULL 至于指针(地址值),根据实际选择用0.NULL还是nullptr.对使用了C++11特性的项目,选用nullptr:对于C++03项目,推荐NULL,因为它像是一个指针
- Linux发展史
简述 Linux是一套自由加开放源代码的类Unix操作系统,诞生于1991年10月5日(第一次正式向外公布),由芬兰学生Linus Torvalds和后来陆续加入的众多爱好者共同开发完成. Linux ...
- 143. Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- iOS8适配工作
1 按钮,菜单文字被加上下划线的问题. 2 状态栏被遮挡的问题.(iPhone6明显,iPhone4S无) 3 使用xcode6最新版本进行编译出现的通知无法呈现的问题(也不进行提示) 4 权限检测( ...
- html5的改进与沿革
HTML5提供了一些新的元素和属性,例如<nav>(网站导航块)和<footer>.这种标签将有利于搜索引擎的索引整理,同时更好的帮助小屏幕装置和视障人士使用,除此之外,还为其 ...
- leetcode 40 Combination Sum II --- java
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 骑士问题(knight) (BFS)
题目描述 在一个标准8×8的国际象棋棋盘上,棋盘中有些格子可能是有障碍物的.已知骑士的初始位置和目标位置,你的任务是计算出骑士最少需要多少步可以从初始位置到达目标位置.有障碍物的格子当然不可以到达. ...
- Python Web.py
安装Web.py root@bt:~# sudo pip install web.py Downloading/unpacking web.py Downloading web.py-0.37.tar ...
- 写出bool,int,float,指针与零值比较的if语句
这个里面float与零值的比较颇有些意思. bool: bool flag; if (flag == true) return; int: int var; if (var == 0) { retur ...
- cve-2015-1635 poc
import socket import random ipAddr = "10.1.89.20" hexAllFfff = " req1 = "GET / H ...