前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1500. Design a File Sharing System(哈希map+优先队列)

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

作者头像
Michael阿明
发布2021-02-19 10:29:12
4800
发布2021-02-19 10:29:12
举报
文章被收录于专栏:Michael阿明学习之路

文章目录

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:

代码语言:javascript
复制
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. 解题

代码语言:javascript
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 题目
  • 2. 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档