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

广州市花都区网站建设公司优秀的网络搜索引擎营销案例

广州市花都区网站建设公司,优秀的网络搜索引擎营销案例,动漫新闻资讯站,建站平台需要授权吗我在上周的笔试中遇到了这样一道题目,觉得有难度而且很考验数据结构与算法的功底,因此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.jinmujx.cn/news/109168.html

相关文章:

  • 张家港优化网站seoseo研究中心好客站
  • 网站开发维护成本计算友情链接有用吗
  • 郴州网站设计专业郑州企业网站建设
  • 洛阳网站建设公司软文营销的作用有哪些
  • 网站制作 意向单免费com域名注册永久
  • 用什么网站做头像同城引流用什么软件
  • 设计平面图沈阳seo博客
  • 免费微网站建设上海网络推广外包公司
  • 中国网站建设服务中心seo优化的主要内容
  • 公司网站用哪个软件做天琥设计培训学校官网
  • 安江县政府网站建设方案网络营销与直播电商专业就业前景
  • 廊坊网站自助建站web网页制作教程
  • 网站设计的基本过程优化设计方法
  • 网站安装出现dirseo的范畴是什么
  • 做网站项目链接提取视频的网站
  • 杨家坪网站建设常德网站设计
  • 搭建服务器做网站长沙网站排名推广
  • 上海网站建设哪家好拼多多seo怎么优化
  • 沈阳酒店企业网站制作网络营销的发展概述
  • app设计网站北京网站建设
  • 如何做旅游小视频网站爱站网关键词挖掘工具站长工具
  • 做电商网站必需知道qc电商平台推广费用大概要多少
  • 网站分析怎么写关键词分类工具
  • 工程施工行业在哪个网站容易找事做百度竞价排名怎么靠前
  • 服务器怎么做网站扬州网站推广公司
  • 盐城网站开发如何企业网站推广建议
  • 长春公司做网站东莞整站优化排名
  • 群晖ds1817做网站什么平台可以免费打广告
  • 设计网站技术台州网站建设平台
  • 用网站做淘宝客怎么样域名注册查询入口