-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
design-video-sharing-platform.cpp
84 lines (74 loc) · 2.25 KB
/
design-video-sharing-platform.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Time: ctor: O(1)
// upload: O(logn+l)
// remove: O(logn)
// like: O(1)
// dislike: O(1)
// view: O(l)
// getLikesAndDislikes: O(1)
// getViews: O(1)
// Space: O(n * l), n = len(videos), l = max(len(v) for v in videos)
// design, heap
class VideoSharingPlatform {
public:
VideoSharingPlatform() {
}
int upload(string video) {
int i;
if (!empty(avails_)) {
i = avails_.top(); avails_.pop();
} else {
i = size(videos_);
videos_.emplace_back();
likes_.emplace_back();
dislikes_.emplace_back();
views_.emplace_back();
}
videos_[i] = video;
return i;
}
void remove(int videoId) {
if (videoId >= size(videos_) || views_[videoId] == -1) {
return;
}
avails_.emplace(videoId);
videos_[videoId].clear();
likes_[videoId] = dislikes_[videoId] = views_[videoId] = 0;
}
string watch(int videoId, int startMinute, int endMinute) {
if (videoId >= size(videos_) || empty(videos_[videoId])) {
return "-1";
}
++views_[videoId];
return videos_[videoId].substr(startMinute, endMinute - startMinute + 1);
}
void like(int videoId) {
if (videoId >= size(videos_) || empty(videos_[videoId])) {
return;
}
++likes_[videoId];
}
void dislike(int videoId) {
if (videoId >= size(videos_) || empty(videos_[videoId])) {
return;
}
++dislikes_[videoId];
}
vector<int> getLikesAndDislikes(int videoId) {
if (videoId >= size(videos_) || empty(videos_[videoId])) {
return {-1};
}
return {likes_[videoId], dislikes_[videoId]};
}
int getViews(int videoId) {
if (videoId >= size(videos_) || empty(videos_[videoId])) {
return -1;
}
return views_[videoId];
}
private:
priority_queue<int, vector<int>, greater<int>> avails_;
vector<string> videos_;
vector<int> likes_;
vector<int> dislikes_;
vector<int> views_;
};