-
Notifications
You must be signed in to change notification settings - Fork 17
/
design-video-sharing-platform.py
98 lines (69 loc) · 2.29 KB
/
design-video-sharing-platform.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import heapq
from dataclasses import dataclass
from typing import Dict, List
@dataclass
class Video:
content: str
likes: int = 0
dislikes: int = 0
views: int = 0
def __len__(self) -> int:
return len(self.content)
class VideoSharingPlatform:
_videos: Dict[int, Video]
_available_ids: List[int]
def __init__(self):
self._videos = {}
self._available_ids = []
self._max_id = 0
def _pick_id(self) -> int:
if self._available_ids:
return heapq.heappop(self._available_ids)
video_id = self._max_id
self._max_id += 1
return video_id
def upload(self, video: str) -> int:
video_id = self._pick_id()
self._videos[video_id] = Video(video)
return video_id
def remove(self, videoId: int) -> None:
if videoId not in self._videos:
return
self._videos.pop(videoId)
heapq.heappush(self._available_ids, videoId)
def watch(self, videoId: int, startMinute: int, endMinute: int) -> str:
if videoId not in self._videos:
return "-1"
video = self._videos[videoId]
video.views += 1
return (
video.content[min(startMinute, len(video)) : min(endMinute + 1, len(video))]
or "-1"
)
def like(self, videoId: int) -> None:
if videoId not in self._videos:
return
self._videos[videoId].likes += 1
def dislike(self, videoId: int) -> None:
if videoId not in self._videos:
return
self._videos[videoId].dislikes += 1
def getLikesAndDislikes(self, videoId: int) -> List[int]:
if videoId not in self._videos:
return [-1]
video = self._videos[videoId]
return [video.likes, video.dislikes]
def getViews(self, videoId: int) -> int:
if videoId not in self._videos:
return -1
video = self._videos[videoId]
return video.views
# Your VideoSharingPlatform object will be instantiated and called as such:
# obj = VideoSharingPlatform()
# param_1 = obj.upload(video)
# obj.remove(videoId)
# param_3 = obj.watch(videoId,startMinute,endMinute)
# obj.like(videoId)
# obj.dislike(videoId)
# param_6 = obj.getLikesAndDislikes(videoId)
# param_7 = obj.getViews(videoId)