网上看到了一道关于bloomburg的面试题,follow 评论的思路 自己试着写了一个HashHeap的实现。
基本思路是维护一个大小为K的最小堆,里面是topK股价变动的公司ID(假设ID是Integer)
HashHeap中维护一个companyIdHeap 之外还有一个HashMap 它的key 是CompanyId, value是Company信息 此处我假设company信息包含这个company在companyIdHeap中的index (这个index可以使delete function的复杂度降到O(logK))和它的股价变化了多少次
每当有新的估价更新,假设应用会调用HashHeap的add函数 输入是当前股价变化的公司ID 然后更新 hashmap 和 根据当前heap size更新companyIdHeap
在更新companyIdHeap的时候 如果这个变化的公司的股价变化次数大于最小堆的root 那么替换掉这个root 然后从0向下调整heap heap调整的比较条件是看谁的股价变化次数少
delete函数直接delete掉一个company O(logK)
poll函数是给heap root的company的股价变化次数减一(其实还没想好这个函数具体应该做什么 暂时把它放在注释里面)
不足之处请指出! 多谢!
package Heap; import java.util.*; public class HashHeap {
//a list of companyId
private List<Integer> companyIdHeap;
private int size_t;
//map from companyId to Node {index in heap and frequent}
private Map<Integer, Node> companyToFrequent;
private String mode; //min or max
private int K;
class Node{
public int index;
public int frequent;
public Node (int index, int fre){
this.index= index;
this.frequent = fre;
}
public Node(Node node){
this.index = node.index;
this.frequent = node.frequent;
}
} public HashHeap(int K){
this.companyIdHeap = new ArrayList<Integer>();
this.size_t =0;
this.companyToFrequent = new HashMap<Integer, Node>();
mode = "min";
this.K = K;
} public int peek(){
if(!companyIdHeap.isEmpty())
return companyIdHeap.get(0);
return -1;
} public int size(){
return size_t;
} public boolean empty(){
return companyIdHeap.size()==0;
} public int parent(int id){
if(id==0){
return -1;
}
return (id-1)/2;
} public int lson(int id){
return 2*id +1;
} public int rson(int id){
return 2*id+2;
} public boolean compare(int companyA, int companyB){
if(companyToFrequent.get(companyA).frequent<companyToFrequent.get(companyB).frequent){
if(mode.equals("min")){
return true;
}else{
return false;
}
}else{
if(mode.equals("min")){
return false;
}else{
return true;
}
}
} public void swap(int indexA, int indexB){
int companyA = companyIdHeap.get(indexA);
int companyB = companyIdHeap.get(indexB);
Node compNodeA = companyToFrequent.get(companyA);
Node compNodeB = companyToFrequent.get(companyB);
companyIdHeap.set(indexA, companyB);
companyIdHeap.set(indexB, companyA);
companyToFrequent.put(companyA, new Node(indexB, compNodeA.frequent));
companyToFrequent.put(companyB, new Node(indexA, compNodeB.frequent));
} public void siftup(int index){
while(parent(index)>-1){
int parent = parent(index);
if(compare(companyIdHeap.get(parent), companyIdHeap.get(index))){
break;
}else{
swap(parent, index);
}
index = parent;
}
} public void siftdown(int index){
while(lson(index)< companyIdHeap.size()){
int leftSon = lson(index);
int rightSon = rson(index);
int son = rightSon;
if(rightSon>=companyIdHeap.size()||compare(companyIdHeap.get(leftSon),companyIdHeap.get(rightSon))){
son = leftSon;
}
if(compare(companyIdHeap.get(index), companyIdHeap.get(son))){
break;
}else{
swap(index, son);
}
index=son;
} } public void add(int company){
//update hashmap
if(companyToFrequent.containsKey(company)){
Node node = companyToFrequent.get(company);
companyToFrequent.put(company, new Node(node.index, node.frequent+1));
}else{
companyToFrequent.put(company, new Node(-1, 1));
}
//update heap
Node node = companyToFrequent.get(company);
if(this.size_t==K){
//if heap need to be updated
if(compare(peek(), company)){
companyIdHeap.set(0, company);
companyToFrequent.put(company, new Node(0, node.frequent));
siftdown(0);
}
return;
}
companyIdHeap.add(company);
size_t++;
companyToFrequent.put(company, new Node(companyIdHeap.size()-1, node.frequent));
siftup(companyIdHeap.size()-1);
} public void delete(int company){
if(companyToFrequent.containsKey(company)){
Node node = companyToFrequent.get(company);
int index = node.index;
swap(index, companyIdHeap.size()-1);
companyIdHeap.remove(companyIdHeap.size()-1);
companyToFrequent.remove(company);
size_t--;
//the condition will be false if index == companyIdHeap.size()-1 before
if(index<companyIdHeap.size()){
siftup(index);
siftdown(index);
}
}
} /*public int poll(){
int res = companyIdHeap.get(0);
Node node = companyToFrequent.get(res);
if(node.frequent==1){
size_t--;
swap(0, companyIdHeap.size()-1);
companyIdHeap.remove(companyIdHeap.size()-1);
companyToFrequent.remove(res);
// the condition will be true is companyIdHeap.size() == 1 before
if(companyIdHeap.size()>0){
siftdown(0);
}
}else{
companyToFrequent.put(res, new Node(0, node.frequent-1));
} return res;
}*/
}