面试准备 - 最大堆的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的基础知识,就是明 ...
随机推荐
- C# final project
Problem Statement You are tasked with developing a task manager. The task manager will allow people ...
- No operation was found with the name {http://impl.service.xq.com/}sayHi
org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name {http://impl.ser ...
- STL练习题
//hdu_2717 //map 一对多映射,基于关键字快速查找,不允许重复值 //queue 队列 先进先出 #include<iostream> #include<cstdio& ...
- WM8978和VS1053B的区别
WM8978是DAC和ADC芯片,不具备编解码的功能.不能把MP3格式或者WAV格式的文件解码出来.有播放和录音功能. 录音如果需要保存为MP3或者WAV格式,那么需要进行软件编码. VS1053B是 ...
- Win10 设置外网多用户远程桌面连接
主要原理:利用路由器的虚拟服务器功能,将内网的Ip地址通过端口映射提供给外网,使得外网能够访问到目的主机. 1. 配置路由器上的虚拟服务器,假设目的主机内网的ip为192.168.1.100,则配置如 ...
- 如何让MVC6在IIS上面跑
asp.net5的MVC6发布出来的结果和MVC5之前版本的相差太远了,直接在本地的IIS服务器上面是不可能运行的. 看了汤姆大叔的MVC6项目发布与部署,讲了很多丰富的知识点.但是对于立即要解决问题 ...
- java 并发性和多线程 -- 读感 (一 线程的基本概念部分)
1.目录略览 线程的基本概念:介绍线程的优点,代价,并发编程的模型.如何创建运行java 线程. 线程间通讯的机制:竞态条件与临界区,线程安全和共享资源与不可变性.java内存模型 ...
- linux系统添加硬盘方法
[root@wen /]# fdisk -l[root@wen /]# fdisk /dev/sdb[root@wen /]# mkfs -t ext3 -c /dev/sdb1[root@wen / ...
- js 数组遍历for..in弊端
//for..in在数组中的弊端 原则上数组Array对象是不能操作的,但是有些程序员开始不注意把Array的原型链上添加了方法就会出现意想不到的bug //例如 ,,]; Array.prototy ...
- EQueue - 一个C#写的开源分布式消息队列的总体介绍
前言 本文想介绍一下前段时间在写enode时,顺便实现的一个分布式消息队列equeue.这个消息队列的思想不是我想出来的,而是通过学习阿里的rocketmq后,自己用c#实现了一个轻量级的简单版本.一 ...