当前位置: 首页 > news >正文

泰安市高新区建设局网站网络营销网站建设案例

泰安市高新区建设局网站,网络营销网站建设案例,政务系统网站建设工作先进个人主要事迹,昆明网站设计8888168我在上周的笔试中遇到了这样一道题目,觉得有难度而且很考验数据结构与算法的功底,因此Mark一下。 需求说明 设计并实现一个缓存数据结构: 该数据结构具有以下功能: get(key) 如果指定的key存在于缓存中,则返回与该键关联的值&am…

我在上周的笔试中遇到了这样一道题目,觉得有难度而且很考验数据结构与算法的功底,因此Mark一下。

需求说明

设计并实现一个缓存数据结构:

该数据结构具有以下功能:

get(key)
如果指定的key存在于缓存中,则返回与该键关联的值,则返回-1。

put(key、val、weight)

将值与缓存中的键关联,以便以后可以通过get(key)检索值。

缓存具有固定的容量,当达到该容量时,score最小的密钥必须失效,直到密钥的数量落在缓存容量之内。
score的计算方法如下:

weight ∕ [ln(current_time - last_accessed_time + 1) + 1]

缓存的实现需要get(key)的时间复杂度为O(1)。为了实现高速缓存,您可以假设可用一些常见的数据结构,如数组、不同类型的列表和哈希表。在答案的最后,给出并解释get(key)和放入put(key)的计算复杂度

我的思路

首先,一说到get和put,肯定会想到哈希map,并且哈希的get时间复杂度也为O(1),符合要求,但比较棘手的需求是如何实现缓存的score机制,当缓存满的时候需要让score最低的节点drop掉。苦思冥想之后我想到了优先队列(priority queue),平时觉得这个数据结构很冷门,但确实有应用场景,优先队列是一种根据权重进行出队顺序排列的队列,那么我只需要将题目中的score定位为权重就行了。
此时我又想到了用JAVA中的Comparator去定义一个这样的权重策略,因为优先队列的权重是可以被Comparator重写的。所以我总共需要用到两个数据结构。
用hashmap实现get和put的一一对应,同时将节点存入优先队列,当容量满时让score小的出队就行了。(注意,Java中优先队列是权重小的先出队)

我的答案

import java.util.*;class Node{int key;int val;int weight;int timeStamp;public Node(int key, int val, int weight, int timeStamp) {this.key = key;this.val = val;this.weight = weight;this.timeStamp = timeStamp;}
}public class Cache {int capacity;int timeStamp;Map<Integer,Node> nodeMap;  //k-v mappingPriorityQueue<Node> prque;  //store the nodepublic Cache(int capacity){this.capacity = capacity;this.timeStamp = 0;nodeMap = new HashMap<>();Comparator<Node> timeWeightComparator = new Comparator<Node>() { //rewrite the priority@Overridepublic int compare(Node o1, Node o2) {return (int) (o1.weight / (Math.log(o1.timeStamp - o2.timeStamp + 1) + 1) -(o2.weight / (Math.log(o2.timeStamp - o1.timeStamp + 1) + 1)));}};prque = new PriorityQueue<>(timeWeightComparator);}public int get(int key){  //时间复杂度O(1), hashmap.get为O(1)if (!nodeMap.containsKey(key)){return -1;}Node getNode = nodeMap.get(key);getNode.timeStamp = ++timeStamp;return getNode.val;}void put(int key, int val, int weight){ //最好的情况是已经包含这个键了,时间复杂度为O(1)if (this.capacity <= 0){return;}if (nodeMap.containsKey(key)){Node newNode = nodeMap.get(key);newNode.val = val;newNode.weight = weight;newNode.timeStamp = ++ timeStamp;}else {if (nodeMap.size() == capacity){Node leastNode = prque.poll(); //O(logN)assert leastNode != null;nodeMap.remove(leastNode.key);}Node newNode = new Node(key, val, weight, ++timeStamp);prque.add(newNode);nodeMap.put(key,newNode);}}public static void main(String[] args) { //test caseCache cache = new Cache(5);cache.put(0,15,3);cache.put(1,28,10);cache.put(2,16,4);cache.put(3,4,6);cache.put(4,75,5);cache.put(4,100,100);System.out.println(cache.get(1));System.out.println(cache.get(2));System.out.println(cache.get(3));System.out.println(cache.get(4));System.out.println(cache.get(0));}
}

BigO notation analysis

get

The get operation is base on the hashmap.get(key). So, the time complexity is O(1).

put

The put operation can be seperated to follow two case:

1. Don’t need insert a new node (when the key is exist)

In this case, we only need to get the node from hashmap and update it. The time complexity is O(1).

2. Insert new Node

If the capcity is not reached. we can insert a new node directly. the complexity is O(logN) + O(1) = O(logN) ---- (O(logN) for priorityque, O(1) for hashmap).

If the capicity is reached. we need to poll a node with least score, the time complexity is O(logN). Then inster a new node. The time complexity is O(logN) + O(logN) + O(1) = O(logN).

http://www.yayakq.cn/news/855052/

相关文章:

  • 潮州哪里做网站wordpress 百万级数据
  • 在哪里可以做自己的网站网络推广方案案例
  • seo网站推广首页排名备案 网站 漏接 电话
  • 外贸网站个性设计搜索引擎案例分析结论
  • 网站 app建设开发合作协议手机电脑版下载软件
  • 浙江住房与城乡建设部网站山东网站建设代理
  • 西安长安区网站优化地址公司网站建设意见征集
  • 沪江博客wordpress模板上海网站排名优化费用
  • 免费个人建站系统成都专业网站搭建公司
  • 建设银行官网网站人事如何在交易网站做电子印章
  • 国内网站要备案网站如何才能被百度收录
  • 学做网站论中山网站搜索优化
  • 上海建设行政主管部门政务网站网站建设地图怎么设置
  • 外贸网站源码 php利于seo优化的网站
  • 中英文企业网站模板wordpress中文模板下载地址
  • 长春财经学院教务系统一个网站两个域名 seo
  • 毕设做网站什么主题比较好国内漂亮大气的网站
  • 车公庙做网站移动互联网 网站建设
  • 深圳免费模板建站迁安做网站中的cms润强
  • asp+access网站开发实例精讲建立平台的步骤
  • 关于建设俄语网站的稿子网站建设中图片是什么意思
  • 投资集团网站建设网站设计工具有哪些
  • 学校门户网站建设seo网站优化方案案例
  • 湖南高端网站制作公网站地图生成软件
  • 天津网站建设找哪家视频剪辑教程自学
  • 网站第三方微信登陆怎么做的电脑制作图片的软件
  • 华北理工大学学科建设处网站外贸黄页
  • 企业网站建设的案例广州编程培训机构哪里好
  • 律师网站 扁平化下载优化大师安装桌面
  • 公司网站404门户网站设计特点