当前位置:网站首页>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;
}
}
边栏推荐
- AI security and Privacy Forum issue 11 - stable learning: finding common ground between causal reasoning and machine learning
- Use yolov3 to train yourself to make datasets and get started quickly
- Cube 技术解读 | Cube 渲染设计的前世今生
- Summary of information of main account of Chia Tai futures on Wednesday in advance
- 适配器模式--能不能好好说话?
- Is the securities account opened by qiniu Gang safe and reliable?
- 文件excel导出
- 如何养成一个好习惯?靠毅力?靠决心?都不是!
- Bark – 自己给自己的 iPhone 发推送提醒 – 最简单的推送提醒服务,开源免费
- MySQL optimized learning diary 10 - locking mechanism
猜你喜欢

The application of the spingboot+quartrz production environment supports distributed, custom corn, reflective execution of multiple tasks

设置默认收货地址【项目 商城】

使用Yolov3训练自己制作数据集,快速上手
![Electron desktop development (development of an alarm clock [End])](/img/2b/dd59ebc8d11bedfc53020d69f1aa69.png)
Electron desktop development (development of an alarm clock [End])

使用Yolov5训练好模型调用电脑自带摄像头时出现问题:TypeError: argument of type “int‘ is not iterable的解决方法

Use yolov3 to train yourself to make datasets and get started quickly

Command mode - attack, secret weapon

Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged

Typeerror: argument of type "Int 'is not Iterable

全国多年太阳辐射空间分布数据1981-2022年、气温分布数据、蒸散量数据、蒸发量数据、降雨量分布数据、日照数据、风速数据
随机推荐
Jerry's ble spp open pin_ Code function [chapter]
UCI-HAR数据集的处理
Introduction to thread pool: ThreadPoolExecutor
使用国产MCU(国民技术 N32G031F8S7) 实现 PWM+DMA 控制 WS2812
想做钢铁侠?听说很多大佬都是用它入门的
WordPress用户名修改插件:Username Changer
WordPress landing page customization plug-in recommendation
Jerry's ble spp open pin_ Code function [chapter]
Vscade -- vscode multi window name is configured as project name
WordPress站内链接修改插件:Velvet Blues Update URLs
js合并两个对象(面试题)
SpingBoot+Quartrz生产环境的应用支持分布式、自定义corn、反射执行多任务
【碎碎念】关于波长|波速|周期的想法
js面试题---箭头函数,find和filter some和every
修改 WordPress 管理账号名称插件:Admin renamer extended
在毕设中学习03
数据库系统概论 ---- 第二章 -- 关系数据库(2.1~2.3)(重要知识点)
Method of converting VOC format data set to Yolo format data set
202年最新热门收益较高的年金险产品是什么?
Définir l'adresse de réception par défaut [Centre commercial du projet]