当前位置:网站首页>Count the top k strings with the most occurrences

Count the top k strings with the most occurrences

2022-06-11 11:24:00 N. LAWLIET

problem
The most frequent occurrence of statistics before K A string
Example
A little
thought
Reinforced reactor , Build small root heap and reverse index table , The small root heap makes it easy to replace the smallest value at any time
1.string Of Node How to write Add a new String Find the corresponding node first , If there is no new one , The location of the corresponding node on the heap defaults to -1, If the first logical branch does not exist, it is the first time to come in Node If not, check whether it is on the heap (HashMap)
2. How to build a heap
3. How to build a reverse index table During the exchange, the heap node and the reverse index table are exchanged at the same time (HashMap)
Code

public class Code02_TopK {
    
	
	private Node[] heap;
	private int heapSize;
	private HashMap<String, Node> strNodeMap;
	private HashMap<Node, Integer> indexHashMap;
	private NodeHeapComp comp;
	private TreeSet<Node> treeSet;
	public Code02_TopK(int k) {
    
		heap = new Node[k];
		heapSize = 0;
		strNodeMap = new HashMap<>();
		indexHashMap = new HashMap<>();
		comp = new NodeHeapComp();
		treeSet = new TreeSet<>();
	}
	
	public static class Node {
    
		String str;
		int times;
		public Node(String str,int times) {
    
			this.str = str;
			this.times = times;
		}
	}
	public static class NodeHeapComp implements Comparator<Node>{
    
		@Override
		public int compare(Node o1,Node o2){
    
			return o1.times!=o2.times?(o1.times - o2.times):(o1.str.compareTo(o2.str));
		}
	}
	
	public void add(String str) {
    
		
		Node curNode = null;
		// Corresponding position in the node 
		int preindex = -1;
		if(!strNodeMap.containsKey(str)) {
    
			curNode = new Node(str, 1);
			strNodeMap.put(str, curNode);
			indexHashMap.put(curNode, -1);
		}else {
    
			curNode = strNodeMap.get(str);
			if(treeSet.contains(curNode)) {
    
				treeSet.remove(curNode);
			}
			curNode.times++;
			preindex = indexHashMap.get(curNode);
			
		}
		
		if(preindex == -1) {
    
			if(heapSize == heap.length) {
    
				if(comp.compare(curNode, heap[0])>0) {
    
					treeSet.remove(heap[0]);
					treeSet.add(curNode);
					indexHashMap.put(heap[0], -1);
					indexHashMap.put(curNode, 0);
					heap[0] = curNode;
					heapify(0, heapSize);
				}
			}else{
    
				treeSet.add(curNode);
				indexHashMap.put(curNode, heapSize);
				heap[heapSize] = curNode;
				insertHeap(heapSize++);
			}
		}else {
    
			treeSet.add(curNode);
			heapify(preindex, heapSize);
		}
		
	}
	public List<String> top() {
    
		List<String> ans  = new ArrayList<>();
		for(Node cur:treeSet) {
    
			ans.add(cur.str);
		}
		return ans;
	}
	public void insertHeap(int index) {
    
		if(heap[index]!=null) {
    
			while(comp.compare(heap[index], heap[(index-1)/2])<0) {
    
				swap(index,(index-1)/2);
				index = (index-1)/2;
			}
		}	
	}
	public void heapify(int index,int heapSize) {
    
		int left = index*2+1;
		int right = index*2+2;
		int smallindex = index;
		while(left<heapSize) {
    
			if(comp.compare(heap[left],heap[index])<0) {
    
				smallindex = left;
			}
			if(right<heapSize&&comp.compare(heap[right], heap[smallindex])<0) {
    
				smallindex = right;
			}
			if(smallindex!=index) {
    
				swap(smallindex, index);
			}
			index = smallindex;
			left = index*2+1;
			right = index*2+2;
		}
	}
	public void swap(int index1,int index2) {
    
		indexHashMap.put(heap[index1], index2);
		indexHashMap.put(heap[index2], index1);
		Node tap = heap[index1];
		heap[index1] = heap[index2];
		heap[index2] = tap;
	}
}

原网站

版权声明
本文为[N. LAWLIET]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111109513138.html