- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- Discard stale entries when necessary and process the best live candidate.
Code notes
- 53 lines of Python from the credited upstream file 2254.py.
- The implementation visibly relies on sequence storage, hash lookup, work queue.
- No explicit loop blocks detected.
Complexity
Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class VideoSharingPlatform:2 def __init__(self):3 self.currVideoId = 04 self.usedIds = []5 self.videoIdToVideo = {}6 self.videoIdToViews = collections.Counter()7 self.videoIdToLikes = collections.Counter()8 self.videoIdToDislikes = collections.Counter()9 10 def upload(self, video: str) -> int:11 videoId = self._getVideoId()12 self.videoIdToVideo[videoId] = video13 return videoId14 15 def remove(self, videoId: int) -> None:16 if videoId in self.videoIdToVideo:17 heapq.heappush(self.usedIds, videoId)18 del self.videoIdToVideo[videoId]19 del self.videoIdToViews[videoId]20 del self.videoIdToLikes[videoId]21 del self.videoIdToDislikes[videoId]22 23 def watch(self, videoId: int, startMinute: int, endMinute: int) -> str:24 if videoId not in self.videoIdToVideo:25 return '-1'26 self.videoIdToViews[videoId] += 127 video = self.videoIdToVideo[videoId]28 return video[startMinute:min(endMinute + 1, len(video))]29 30 def like(self, videoId: int) -> None:31 if videoId in self.videoIdToVideo:32 self.videoIdToLikes[videoId] += 133 34 def dislike(self, videoId: int) -> None:35 if videoId in self.videoIdToVideo:36 self.videoIdToDislikes[videoId] += 137 38 def getLikesAndDislikes(self, videoId: int) -> list[int]:39 if videoId in self.videoIdToVideo:40 return [self.videoIdToLikes[videoId], self.videoIdToDislikes[videoId]]41 return [-1]42 43 def getViews(self, videoId: int) -> int:44 if videoId in self.videoIdToVideo:45 return self.videoIdToViews[videoId]46 return -147 48 def _getVideoId(self) -> int:49 if not self.usedIds:50 self.currVideoId += 151 return self.currVideoId - 152 return heapq.heappop(self.usedIds)53