[LeetCode] Design Video Sharing Platform

2254. Design Video Sharing Platform

You have a video sharing platform where users can upload and delete videos. Each video is a string of digits, where the ith digit of the string represents the content of the video at minute i. For example, the first digit represents the content at minute 0 in the video, the second digit represents the content at minute 1 in the video, and so on. Viewers of videos can also like and dislike videos. Internally, the platform keeps track of the number of views, likes, and dislikes on each video.

When a video is uploaded, it is associated with the smallest available integer videoId starting from 0. Once a video is deleted, the videoId associated with that video can be reused for another video.

Implement the VideoSharingPlatform class:

  • VideoSharingPlatform() Initializes the object.
  • int upload(String video) The user uploads a video. Return the videoId associated with the video.
  • void remove(int videoId) If there is a video associated with videoId, remove the video.
  • String watch(int videoId, int startMinute, int endMinute) If there is a video associated with videoId, increase the number of views on the video by 1 and return the substring of the video string starting at startMinute and ending at min(endMinute, video.length - 1) (inclusive). Otherwise, return “-1”.
  • void like(int videoId) Increases the number of likes on the video associated with videoId by 1 if there is a video associated with videoId.
  • void dislike(int videoId) Increases the number of dislikes on the video associated with videoId by 1 if there is a video associated with videoId.
  • int[] getLikesAndDislikes(int videoId) Return a 0-indexed integer array values of length 2 where values[0] is the number of likes and values[1] is the number of dislikes on the video associated with videoId. If there is no video associated with videoId, return [-1].
  • int getViews(int videoId) Return the number of views on the video associated with videoId, if there is no video associated with videoId, return -1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class VideoSharingPlatform {
int id = 0;
set<int> rm;
unordered_map<int, string> v;
unordered_map<int, array<int,3>> meta;
public:
VideoSharingPlatform() {}

int upload(string video) {
int ID = rm.empty() ? id++ : *begin(rm);
rm.erase(ID);
v[ID] = video;
meta[ID] = {0,0,0};
return ID;
}

void remove(int videoId) {
if(v.count(videoId)) {
v.erase(videoId);
meta.erase(videoId);
rm.insert(videoId);
}
}

string watch(int videoId, int startMinute, int endMinute) {
if(!v.count(videoId)) return "-1";
auto video = v[videoId];
if(video.length() <= startMinute) return "-1";
meta[videoId][2]++;

return video.substr(startMinute, min(endMinute + 1, (int)video.length()) - startMinute);
}

void like(int videoId) {
if(v.count(videoId))
meta[videoId][0]++;
}

void dislike(int videoId) {
if(v.count(videoId))
meta[videoId][1]++;
}

vector<int> getLikesAndDislikes(int videoId) {
if(!v.count(videoId)) return {-1};
return {meta[videoId][0], meta[videoId][1]};
}

int getViews(int videoId) {
if(!v.count(videoId)) return -1;
return meta[videoId][2];
}
};

/**
* Your VideoSharingPlatform object will be instantiated and called as such:
* VideoSharingPlatform* obj = new VideoSharingPlatform();
* int param_1 = obj->upload(video);
* obj->remove(videoId);
* string param_3 = obj->watch(videoId,startMinute,endMinute);
* obj->like(videoId);
* obj->dislike(videoId);
* vector<int> param_6 = obj->getLikesAndDislikes(videoId);
* int param_7 = obj->getViews(videoId);
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/01/PS/LeetCode/design-video-sharing-platform/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.