- 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
- 67 lines of C++ from the credited upstream file 2254.cpp.
- 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 public:3 int upload(string video) {4 const int videoId = getVideoId();5 videoIdToVideo[videoId] = video;6 return videoId;7 }8 9 void remove(int videoId) {10 if (videoIdToVideo.contains(videoId)) {11 usedIds.push(videoId);12 videoIdToVideo.erase(videoId);13 videoIdToViews.erase(videoId);14 videoIdToLikes.erase(videoId);15 videoIdToDislikes.erase(videoId);16 }17 }18 19 string watch(int videoId, int startMinute, int endMinute) {20 const auto it = videoIdToVideo.find(videoId);21 if (it == videoIdToVideo.cend())22 return "-1";23 ++videoIdToViews[videoId];24 const string video = it->second;25 const int duration =26 min(endMinute, static_cast<int>(video.length()) - 1) - startMinute + 1;27 return video.substr(startMinute, duration);28 }29 30 void like(int videoId) {31 if (videoIdToVideo.contains(videoId))32 ++videoIdToLikes[videoId];33 }34 35 void dislike(int videoId) {36 if (videoIdToVideo.contains(videoId))37 ++videoIdToDislikes[videoId];38 }39 40 vector<int> getLikesAndDislikes(int videoId) {41 return videoIdToVideo.contains(videoId)42 ? vector<int>{videoIdToLikes[videoId],43 videoIdToDislikes[videoId]}44 : vector<int>{-1};45 }46 47 int getViews(int videoId) {48 return videoIdToVideo.contains(videoId) ? videoIdToViews[videoId] : -1;49 }50 51 private:52 int currVideoId = 0;53 priority_queue<int, vector<int>, greater<>> usedIds;54 unordered_map<int, string> videoIdToVideo;55 unordered_map<int, int> videoIdToViews;56 unordered_map<int, int> videoIdToLikes;57 unordered_map<int, int> videoIdToDislikes;58 59 int getVideoId() {60 if (usedIds.empty())61 return currVideoId++;62 const int minUsedId = usedIds.top();63 usedIds.pop();64 return minUsedId;65 }66};67