面试准备 - 最大堆的Csharp实现
面试中最常见的问题之一。。。在N个数中间寻找前K大个元素
最常见的解法就是最大堆 时间复杂度O(N*log(K)) 空间复杂度O(k)
实现了一个最简单的最大堆,每次有元素进来都和堆顶元素比较一下,如果新元素比较大就替换,然后就逐级更新到堆底
namespace Clover.Algoritms.DataStructure
{
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading; using Clover.Algoritms.Common; public class MaxHeap
{
public double[] items; public int count = ; public MaxHeap(int capacity)
{
if (capacity <= )
{
throw new ArgumentOutOfRangeException("capacity");
}
this.items = new double[capacity];
for (int i = ; i < this.items.Length; i++)
{
this.items[i] = double.MinValue;
}
} public bool Validate()
{
for (int i = ; i < this.items.Length; i++)
{
int left = * i + ;
int right = * i + ;
if (left < this.items.Length)
{
if (this.items[left] > this.items[i])
{
return false;
}
}
if (right < this.items.Length)
{
if (this.items[right] > this.items[i])
{
return false;
}
}
}
return true;
} public void MaxHeapify(int i, int size = -)
{
var s = size > ? size : items.Length;
if (i >= s)
{
return;
} var l = this.left(i);
var r = this.right(i);
var largest = i;
if (l < s && items[l] > items[i])
{
largest = l;
}
if (r < s && items[r] > items[largest])
{
largest = r;
}
if (largest != i)
{
var temp = items[i];
items[i] = items[largest];
items[largest] = temp;
MaxHeapify(largest);
}
} public void BuildMaxHeap()
{
for (int i = items.Length / ; i >= ; i--)
{
this.MaxHeapify(i);
}
} public int left(int i)
{
return i * + ;
} public int right(int i)
{
return i * + ;
} public int parent(int i)
{
return i / - ;
} public void HeapSort()
{
this.BuildMaxHeap();
for (int i = items.Length / ; i >= ; i--)
{
var temp = items[];
items[] = items[i];
items[i] = temp;
var size = items.Length - - items.Length / + i;
this.MaxHeapify(i, size);
}
} //max heap is used to find top k smallest items.
public void PickTopN(double d)
{
if (count < items.Length)
{
items[count] = d;
count++;
if (count >= items.Length)
{
this.BuildMaxHeap();
}
}
else if (d < items[])
{
items[] = d;
this.MaxHeapify();
}
} public double Maximun()
{
if (count == )
{
throw new Exception("there is no any element in heap");
} return items[];
} public double HeapExtractMax()
{
if (count == )
{
throw new Exception("there is no any element in heap");
}
var max = items[];
items[] = items[count];
count--;
this.MaxHeapify();
return max;
} public void MaxHeapInsnsert(double d)
{
count++;
double[] newItems = new double[count];
for (int i = ; i < count - ; i++)
{
newItems[i] = items[i];
}
newItems[count - ] = double.MinValue;
items = newItems;
MaxHeapIncreaseKey(count - , d);
} private void MaxHeapIncreaseKey(int ind, double d)
{
var i = ind;
if (d < items[i])
{
throw new Exception("new key is smaller than than current key");
}
items[i] = d;
while (i > && items[this.parent(i)] < items[i])
{
ObjectExtension.Exhange(ref items[i], ref items[this.parent(i)]);
i = this.parent(i);
}
}
}
}
面试准备 - 最大堆的Csharp实现的更多相关文章
- 面试经典算法:优先队列,最大堆,堆排序,左偏树Golang实现
堆排序 使用优先队列-最小/最大堆可实现. 优先队列 优先队列是一种能完成以下任务的队列:插入一个数值,取出最小的数值(获取数值,并且删除).优先队列可以用二叉树来实现,我们称这种为二叉堆. 最小堆 ...
- 记2016腾讯 TST 校招面试经历,电面、笔试写代码、技术面、hr面,共5轮
(出处:http://www.cnblogs.com/linguanh/) 前序: 距离 2016 腾讯 TST 校招面试结束已经5天了,3月27日至今,目前还在等待消息.从投简历到两轮电面,再到被 ...
- Android面试一天一题(1Day)
写在前面 该博客思路源于在简书看到goeasyway博主写的Android面试一天一题系列,无copy之意,仅为让自己总结知识点,成长一点点.先感谢各位大神的无私分享~! 关于题目,大部分则出自And ...
- 转:最近5年133个Java面试问题列表
最近5年133个Java面试问题列表 Java 面试随着时间的改变而改变.在过去的日子里,当你知道 String 和 StringBuilder 的区别就能让你直接进入第二轮面试,但是现在问题变得越来 ...
- 面试题目: PHP 有哪些优缺点?
当面试官噼里啪啦的问你一大堆问题后,突然问你,PHP有哪些优缺点?你蒙了没? 反正我是蒙了,不管你信不信! 现在,关于PHP优缺点,大致的说几点: 1. 语法简单的,上手很快,而且还有很多很便捷的开 ...
- 我的游戏蜗牛web前端面试经历
蜗牛在江苏苏州地区应该算是比较大的互联网公司了,可以称得上中国游戏的鼻祖,之前一直很想进蜗牛,但作为一个应届毕业生却没有看到蜗牛发布任何关于招聘实习生的职位,无奈之下于是就毛遂自荐了,主动以邮件的形式 ...
- python面试大全
问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...
- python面试2
Python语言特性 1 Python的函数参数传递 看两个例子: 1 2 3 4 5 a = 1 def fun(a): a = 2 fun(a) print a # 1 1 2 ...
- 在帝都的Android面试感想
#第一次面试赤子城Android开发实习生 关于面试的表现和感想 1.没有准备充分就去面试(这是大忌,也就直接决定了结果) 我去面试Android,但是却不知道很多关于Android的基础知识,就是明 ...
随机推荐
- 练习1-16:修改打印最长文本行的程序的主程序main,使之可以打印任意长度的输入行的长度,并尽可能多地打印文本(C程序设计语言 第2版)
该书英文配套答案 Answer to Exercise -, page Revise the main routine of the longest-line program so it will c ...
- unity发布安卓 截图保存到本地
using System.IO; //获取系统时间并命名相片名 System.DateTime now = System.DateTime.Now; string times = now.ToStri ...
- caffe 在window下编译(windows7, cuda8.0,matlab接口编译)
1. 环境:Windows7,Cuda8.0,显卡GTX1080,Matlab2016a,VS2013 (ps:老板说服务器要装windows系统,没办法,又要折腾一番,在VS下编译好像在cuda8. ...
- UNITY 状态机 + SVN + 码云 下篇
上篇说到自己写的一个FSM状态机,这篇写怎么把代码和码云联系在一起! 首先,我们应该知道为什么使用码云? 码云是开源中国社区2013年推出的基于 Git 的完全免费的代码托管服务,这个服务是基于 Gi ...
- mysql5.7 代价模型浅析
代价模型 mysql 5.7.10代价计算相对之前的版本有5.7 代价模型浅析较大的改进.例如 代价模型参数可以动态配置,可以适应不同的硬件 区分考虑数据在内存和在磁盘中的代价 代价精度提升为浮点型 ...
- Redis 队列操作
class Program { //版本2:使用Redis的客户端管理器(对象池) public static IRedisClientsManager redisClientManager = ne ...
- 在ABP中通过EF直接执行原生Sql的解决方案
一般情况下,使用EF中的查询语法和方法语法可以帮助我们完成绝大部分业务,但是也有特殊的情况需要直接执行Sql语句.比如,我们的业务过于复杂繁琐,或是有些业务使用EF操作时比较复杂,但是使用Sql时会很 ...
- java 多线程(threadlocal)
package com.example; import java.util.Random; public class App { public static class MyRunnable1 imp ...
- Linux 2.6 源码学习-内存管理-buddy算法
核心数据结构 linux 2.6 的内存管理支持NUMA(Non Uniform Memory Access Achitecture),即非一致内存访问体系,在该体系中存在多个CPU,并且拥有分离的存 ...
- 设计模式之美:Private Class Data(私有类数据)
索引 意图 结构 参与者 适用性 效果 实现 实现方式(一):实现对初始化数据的封装. 意图 封装类的初始化数据,控制对类的属性的更改,并保持类数据与使用数据的方法间的隔离. Encapsulate ...