LeetCode 1500. Design a File Sharing System(哈希map+优先队列)

文章目录

    • 1. 题目
    • 2. 解题

1. 题目

We will use a file-sharing system to share a very large file which consists of m small chunks with IDs from 1 to m.

When users join the system, the system should assign a unique ID to them. The unique ID should be used once for each user, but when a user leaves the system, the ID can be reused again.

Users can request a certain chunk of the file, the system should return a list of IDs of all the users who own this chunk. If the user receive a non-empty list of IDs, they receive the requested chunk successfully.

Implement the FileSharing class:

  • FileSharing(int m) Initializes the object with a file of m chunks.
  • int join(int[] ownedChunks): A new user joined the system owning some chunks of the file, the system should assign an id to the user which is the smallest positive integer not taken by any other user. Return the assigned id.
  • void leave(int userID): The user with userID will leave the system, you cannot take file chunks from them anymore.
  • int[] request(int userID, int chunkID): The user userID requested the file chunk with chunkID. Return a list of the IDs of all users that own this chunk sorted in ascending order.

Follow-ups:

  • What happens if the system identifies the user by their IP address instead of their unique ID and users disconnect and connect from the system with the same IP?
  • If the users in the system join and leave the system frequently without requesting any chunks, will your solution still be efficient?
  • If all each user join the system one time, request all files and then leave, will your solution still be efficient?
  • If the system will be used to share n files where the ith file consists of m[i], what are the changes you have to do?

Example:

Input:
["FileSharing","join","join","join","request","request","leave","request","leave","join"]
[[4],[[1,2]],[[2,3]],[[4]],[1,3],[2,2],[1],[2,1],[2],[[]]]
Output:
[null,1,2,3,[2],[1,2],null,[],null,1]
Explanation:
FileSharing fileSharing = new FileSharing(4); 
// We use the system to share a file of 4 chunks.fileSharing.join([1, 2]);    
// A user who has chunks [1,2] joined the system, assign id = 1 to them and return 1.fileSharing.join([2, 3]);    
// A user who has chunks [2,3] joined the system, assign id = 2 to them and return 2.fileSharing.join([4]);       
// A user who has chunk [4] joined the system, assign id = 3 to them and return 3.fileSharing.request(1, 3);   
// The user with id = 1 requested the third file chunk, as only the user with id = 2 has the file, return [2] . Notice that user 1 now has chunks [1,2,3].fileSharing.request(2, 2);   
// The user with id = 2 requested the second file chunk, users with ids [1,2] have this chunk, thus we return [1,2].fileSharing.leave(1);        
// The user with id = 1 left the system, all the file chunks with them are no longer available for other users.fileSharing.request(2, 1);   
// The user with id = 2 requested the first file chunk, no one in the system has this chunk, we return empty list [].fileSharing.leave(2);        
// The user with id = 2 left the system.fileSharing.join([]);        
// A user who doesn't have any chunks joined the system, assign id = 1 to them and return 1. Notice that ids 1 and 2 are free and we can reuse them.Constraints:
1 <= m <= 10^5
0 <= ownedChunks.length <= min(100, m)
1 <= ownedChunks[i] <= m
Values of ownedChunks are unique.
1 <= chunkID <= m
userID is guaranteed to be a user in the system if you assign the IDs correctly. 
At most 10^4 calls will be made to join, leave and request.
Each call to leave will have a matching call for join.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-a-file-sharing-system
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

class FileSharing {priority_queue<int,vector<int>,greater<int>> numCanBeUsed;//小号优先unordered_map<int,set<int>> user_file;unordered_map<int,set<int>> file_user;
public:FileSharing(int m) {for(int i = 1; i <= m; ++i)numCanBeUsed.push(i);}int join(vector<int> ownedChunks) {if(numCanBeUsed.empty()) return -1;int id = numCanBeUsed.top();//取号numCanBeUsed.pop();for(int f : ownedChunks){user_file[id].insert(f);file_user[f].insert(id);}return id;}void leave(int userID) {numCanBeUsed.push(userID);//号放回for(int f : user_file[userID])file_user[f].erase(userID);user_file.erase(userID);}vector<int> request(int userID, int chunkID) {vector<int> ans(file_user[chunkID].begin(), file_user[chunkID].end());if(!ans.empty() && !user_file[userID].count(chunkID)){	//如果文件存在,且 该用户没有该文件user_file[userID].insert(chunkID);file_user[chunkID].insert(userID);}return ans;}
};

1256 ms 140.3 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/474691.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mupdf java_mupdf库学习

http://macleo.iteye.com/blog/1544948Java代码freetye-config --cflags2.C语言的用处其实还是很大的....C也一样&#xff0c;不经意处C语言还在发挥着巨大的作用3.踏破铁鞋无觅处啊http://zhiweiofli.iteye.com/blog/9050694.Bitmap旋转http://blog.sina.com.cn/s/blog_625def…

LeetCode 425. 单词方块(Trie树+DFS)

文章目录1. 题目2. 解题1. 题目 给定一个单词集合 &#xff08;没有重复&#xff09;&#xff0c;找出其中所有的 单词方块 。 一个单词序列形成了一个有效的单词方块的意思是指从第 k 行和第 k 列 (0 ≤ k < max(行数, 列数)) 来看都是相同的字符串。 例如&#xff0c;单…

LeetCode 642. 设计搜索自动补全系统(Trie树)

文章目录1. 题目2. 解题1. 题目 为搜索引擎设计一个搜索自动补全系统。 用户会输入一条语句&#xff08;最少包含一个字母&#xff0c;以特殊字符 ‘#’ 结尾&#xff09;。 除 ‘#’ 以外用户输入的每个字符&#xff0c;返回历史中热度前三并以当前输入部分为前缀的句子。下面…

python 动漫卡通人物图片大全_用Python把人物头像动漫化,不同的表情给你不同的惊喜...

前言最近上网冲浪的时候看到了一个有趣的东西&#xff0c;叫做『人物动漫化』&#xff0c;作为老大的粉丝&#xff0c;怎么可能放过这个机会&#xff0c;让我们先看看效果图&#xff1a;这就是这次要用Python搞的事情啦&#xff0c;我们会利用百度AI的人物动漫化技术&#xff0…

LeetCode MySQL 1264. 页面推荐(union)

文章目录1. 题目2. 解题1. 题目 朋友关系列表&#xff1a; Friendship ------------------------ | Column Name | Type | ------------------------ | user1_id | int | | user2_id | int | ------------------------ 这张表的主键是 (user1_id, use…

LeetCode MySQL 1070. 产品销售分析 III(group by 陷阱)

文章目录1. 题目2. 解题1. 题目 销售表 Sales&#xff1a; -------------------- | Column Name | Type | -------------------- | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | -------------…

LeetCode MySQL 1364. 顾客的可信联系人数量

文章目录1. 题目2. 解题1. 题目 顾客表&#xff1a;Customers ------------------------ | Column Name | Type | ------------------------ | customer_id | int | | customer_name | varchar | | email | varchar | ------------------------ customer_…

LeetCode MySQL 602. 好友申请 II :谁有最多的好友(union all)

文章目录1. 题目2. 解题1. 题目 在 Facebook 或者 Twitter 这样的社交应用中&#xff0c;人们经常会发好友申请也会收到其他人的好友申请。 表 request_accepted 存储了所有好友申请通过的数据记录&#xff0c;其中&#xff0c; requester_id 和 accepter_id 都是用户的编号。…

linux java 共享内存_Linux进程间通信之共享内存

一&#xff0c;共享内存内核管理一片物理内存&#xff0c;允许不同的进程同时映射&#xff0c;多个进程可以映射同一块内存&#xff0c;被多个进程同时映射的物理内存&#xff0c;即共享内存。映射物理内存叫挂接&#xff0c;用完以后解除映射叫脱接。1&#xff0c;共享内存的特…

LeetCode MySQL 1193. 每月交易 I(date_format)

文章目录1. 题目2. 解题1. 题目 Table: Transactions ------------------------ | Column Name | Type | ------------------------ | id | int | | country | varchar | | state | enum | | amount | int | | trans_date …

LeetCode 1257. 最小公共区域(最小公共祖先)

文章目录1. 题目2. 解题1. 题目 给你一些区域列表 regions &#xff0c;每个列表的第一个区域都包含这个列表内所有其他区域。 很自然地&#xff0c;如果区域 X 包含区域 Y &#xff0c;那么区域 X 比区域 Y 大。 给定两个区域 region1 和 region2 &#xff0c;找到同时包含…

创建样式和样式表

css中的样式&#xff08;或者规则&#xff09;由两部分组成&#xff1a;选择器和声明块。每个声明块里面可以包含多条声明&#xff0c;它们之间由分号分隔。如下图所示&#xff1a; 链接外部样式表 外部样式表可以使网页打开的速度更快。但是浏览器在下载外部样式表的时候&…

java 程序输出 赵_编写一个完整的JAVA的程序

编写一个完整的JAVA的程序关注:84 答案:1 mip版解决时间 2021-02-05 08:43提问者妳螚鬧俄螚笑2021-02-05 02:591&#xff0c;接口Person Show()方法输出对象的描述信息。2.类Student 实现Person接口&#xff0c;另有以下属性和方法:属性(私有)String name。方法Student():构造…

LeetCode 1229. 安排会议日程(双指针)

文章目录1. 题目2. 解题1. 题目 你是一名行政助理&#xff0c;手里有两位客户的空闲时间表&#xff1a;slots1 和 slots2&#xff0c;以及会议的预计持续时间 duration&#xff0c;请你为他们安排合适的会议时间。 「会议时间」是两位客户都有空参加&#xff0c;并且持续时间…

LeetCode 1182. 与目标颜色间的最短距离(二分查找/DP)

文章目录1. 题目2. 解题2.1 二分查找2.2 DP1. 题目 给你一个数组 colors&#xff0c;里面有 1、2、 3 三种颜色。 我们需要在 colors 上进行一些查询操作 queries&#xff0c;其中每个待查项都由两个整数 i 和 c 组成。 现在请你帮忙设计一个算法&#xff0c;查找从索引 i 到…

java跳转html页面_springboot 2.0.8 跳转html页面

springboot 成功创建了后&#xff0c;继续写一下跳转到html页面的方法, 这里我把 jsp和html 分开两篇文章。然后再写一个两种方式整合的这篇 是跳转到html文件的1创建目录结果和html文件​ 2配置return 返回模版​ 3.UserController.java代码如下&#xff0c;这里就直接使用上…

LeetCode MySQL 1212. 查询球队积分

文章目录1. 题目2. 解题1. 题目 Table: Teams ------------------------- | Column Name | Type | ------------------------- | team_id | int | | team_name | varchar | ------------------------- 此表的主键是 team_id&#xff0c;表中的每一行都…

Google Maps API 简易教程(四)

Google Maps 类型 一、基本地图类型 Google Maps API支持一下map类型&#xff1a; .ROADMAP&#xff08;正式的&#xff0c;默认为2D地图&#xff09; .SATELLITE&#xff08;逼真的地图&#xff09; .HYBRID(逼真地图道路和城市名) .TERRAIN&#xff08;山脉和河流地图等等&am…

LeetCode MySQL 1077. 项目员工 III

文章目录1. 题目2. 解题1. 题目 项目表 Project&#xff1a; ---------------------- | Column Name | Type | ---------------------- | project_id | int | | employee_id | int | ---------------------- (project_id, employee_id) 是这个表的主键 employee_…

Java什么是重用_深度解析:java必须掌握的知识点——类的重用

类继承的概念和语法类继承的概念根据已有类来定义新类,新类拥有已有类的所有功能。Java只支持类的单继承,每个子类只能有一一个直接超类(父类)。超类是所有子类的公共属性及方法的集合&#xff0c;子类则是超类的特殊化。继承机制可以提高程序的抽象程度&#xff0c;提高代码的…