-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
design-video-sharing-platform.py
97 lines (86 loc) · 2.68 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
# 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)
import heapq
# design, heap
class VideoSharingPlatform(object):
def __init__(self):
self.__avails = []
self.__videos = []
self.__likes = []
self.__dislikes = []
self.__views = []
def upload(self, video):
"""
:type video: str
:rtype: int
"""
if self.__avails:
i = heapq.heappop(self.__avails)
else:
i = len(self.__videos)
self.__videos.append(None)
self.__likes.append(0)
self.__dislikes.append(0)
self.__views.append(0)
self.__videos[i] = video
return i
def remove(self, videoId):
"""
:type videoId: int
:rtype: None
"""
if videoId >= len(self.__videos) or not self.__videos[videoId]:
return
heapq.heappush(self.__avails, videoId)
self.__videos[videoId] = None
self.__likes[videoId] = self.__dislikes[videoId] = self.__views[videoId] = 0
def watch(self, videoId, startMinute, endMinute):
"""
:type videoId: int
:type startMinute: int
:type endMinute: int
:rtype: str
"""
if videoId >= len(self.__videos) or not self.__videos[videoId]:
return "-1"
self.__views[videoId] += 1
return self.__videos[videoId][startMinute:endMinute+1]
def like(self, videoId):
"""
:type videoId: int
:rtype: None
"""
if videoId >= len(self.__videos) or not self.__videos[videoId]:
return
self.__likes[videoId] += 1
def dislike(self, videoId):
"""
:type videoId: int
:rtype: None
"""
if videoId >= len(self.__videos) or not self.__videos[videoId]:
return
self.__dislikes[videoId] += 1
def getLikesAndDislikes(self, videoId):
"""
:type videoId: int
:rtype: List[int]
"""
if videoId >= len(self.__videos) or not self.__videos[videoId]:
return [-1]
return [self.__likes[videoId], self.__dislikes[videoId]]
def getViews(self, videoId):
"""
:type videoId: int
:rtype: int
"""
if videoId >= len(self.__videos) or not self.__videos[videoId]:
return -1
return self.__views[videoId]