面试中最常见的问题之一。。。在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实现的更多相关文章

  1. 面试经典算法:优先队列,最大堆,堆排序,左偏树Golang实现

    堆排序 使用优先队列-最小/最大堆可实现. 优先队列 优先队列是一种能完成以下任务的队列:插入一个数值,取出最小的数值(获取数值,并且删除).优先队列可以用二叉树来实现,我们称这种为二叉堆. 最小堆 ...

  2. 记2016腾讯 TST 校招面试经历,电面、笔试写代码、技术面、hr面,共5轮

    (出处:http://www.cnblogs.com/linguanh/) 前序: 距离  2016 腾讯 TST 校招面试结束已经5天了,3月27日至今,目前还在等待消息.从投简历到两轮电面,再到被 ...

  3. Android面试一天一题(1Day)

    写在前面 该博客思路源于在简书看到goeasyway博主写的Android面试一天一题系列,无copy之意,仅为让自己总结知识点,成长一点点.先感谢各位大神的无私分享~! 关于题目,大部分则出自And ...

  4. 转:最近5年133个Java面试问题列表

    最近5年133个Java面试问题列表 Java 面试随着时间的改变而改变.在过去的日子里,当你知道 String 和 StringBuilder 的区别就能让你直接进入第二轮面试,但是现在问题变得越来 ...

  5. 面试题目: PHP 有哪些优缺点?

    当面试官噼里啪啦的问你一大堆问题后,突然问你,PHP有哪些优缺点?你蒙了没? 反正我是蒙了,不管你信不信! 现在,关于PHP优缺点,大致的说几点: 1.  语法简单的,上手很快,而且还有很多很便捷的开 ...

  6. 我的游戏蜗牛web前端面试经历

    蜗牛在江苏苏州地区应该算是比较大的互联网公司了,可以称得上中国游戏的鼻祖,之前一直很想进蜗牛,但作为一个应届毕业生却没有看到蜗牛发布任何关于招聘实习生的职位,无奈之下于是就毛遂自荐了,主动以邮件的形式 ...

  7. python面试大全

    问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...

  8. python面试2

    Python语言特性 1 Python的函数参数传递 看两个例子:     1 2 3 4 5 a = 1 def fun(a):     a = 2 fun(a) print a  # 1 1 2 ...

  9. 在帝都的Android面试感想

    #第一次面试赤子城Android开发实习生 关于面试的表现和感想 1.没有准备充分就去面试(这是大忌,也就直接决定了结果) 我去面试Android,但是却不知道很多关于Android的基础知识,就是明 ...

随机推荐

  1. 【转】linux shell实现随机数多种方法(date,random,uuid)

    在日常生活中,随机数实际上经常遇到,想丢骰子,抓阄,还有抽签.呵呵,非常简单就可以实现.那么在做程序设计,真的要通过自己程序设计出随机数那还真的不简单了.现在很多都是操作系统内核会提供相应的api,这 ...

  2. FreeRTOS和Ucos在任务优先级的区别

    而ucos的任务优先级是任务优先级的数组越小,任务优先级越高.和STM32的中断优先级保持一样的分析,和freeRTOS相反.

  3. Magicodes.WeiChat——版本发布历史

    购买地址:https://item.taobao.com/item.htm?id=520205558575 您可以在新标签页打开此图,以查看原始图片. Magicodes.WeiChat为湖南心莱信息 ...

  4. 当匿名类型遇上Distinct

    首先定义一个简单类,并重写ToString方法. public class CommidityFilter { public string Property { get; set; } public ...

  5. 可在广域网部署运行的QQ高仿版 -- GG叽叽V3.7,优化视频聊天、控制更多相关细节

    在广域网中,由于网络的结构纷繁复杂.而且其实时状况又是千变万化的,所以,要使广域网中的视频聊天达到一个令人满意的效果,存在诸多挑战.这次发布的GG 3.7版本尝试在这一方向上做一些努力,据我自己测试, ...

  6. 算法:poj1066 宝藏猎人问题。

    package practice; import java.util.Scanner; public class TreasureHunt { public static void main(Stri ...

  7. Nim教程【九】

    向关注这个系列的朋友们,道一声:久违了! 它并没有被我阉掉,他一定会得善终的,请各位不要灰心 Set集合类型 为了在特殊场景下提高程序的性能设置了Set类型,同时也是为了保证性能,所以Set只能容纳有 ...

  8. 用Python编写博客导出工具

    用Python编写博客导出工具 罗朝辉 (http://kesalin.github.io/) CC 许可,转载请注明出处   写在前面的话 我在 github 上用 octopress 搭建了个人博 ...

  9. [ZigBee] 6、ZigBee基础实验——定时器3和定时器4(8 位定时器)

    上一节讲了16位定时器1,本节讲8位定时器3和定时器4! 1.综述 Timer 3 and Timer 4 are two 8-bit timers(8位定时器). Each timer has tw ...

  10. [硬件项目] 2、汽车倒车雷达设计——基于专用倒车雷达芯片GM3101的设计方案与采用CX20106A红外线检测芯片方案对比

    前言 尽管每辆汽车都有后视镜,但不可避免地都存在一个后视镜的盲区,倒车雷达则可一定程度帮助驾驶员扫除视野死角和视线模糊的缺陷,提高驾驶安全性.上一节已经分析清倒车雷达的语音模块(上一节),本节将深入分 ...