Problem solution · C++

Design Video Sharing Platform

Design Video Sharing Platform: a C++ solution using heap or priority queue. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Heap or priority queue
Source
walkccc LeetCode Solutions
Length
67 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Heap or priority queue

For Design Video Sharing Platform, the implementation repeatedly takes the currently best candidate from a heap while inserting newly available choices.

  1. Define the priority key and whether the smallest or largest item should lead.
  2. Push each candidate when it becomes eligible.
  3. 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.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeDesign Video Sharing Platform · C++C++
Use this to learn the idea, then write your own version.
class VideoSharingPlatform { public:  int upload(string video) {    const int videoId = getVideoId();    videoIdToVideo[videoId] = video;    return videoId;  }   void remove(int videoId) {    if (videoIdToVideo.contains(videoId)) {      usedIds.push(videoId);      videoIdToVideo.erase(videoId);      videoIdToViews.erase(videoId);      videoIdToLikes.erase(videoId);      videoIdToDislikes.erase(videoId);    }  }   string watch(int videoId, int startMinute, int endMinute) {    const auto it = videoIdToVideo.find(videoId);    if (it == videoIdToVideo.cend())      return "-1";    ++videoIdToViews[videoId];    const string video = it->second;    const int duration =        min(endMinute, static_cast<int>(video.length()) - 1) - startMinute + 1;    return video.substr(startMinute, duration);  }   void like(int videoId) {    if (videoIdToVideo.contains(videoId))      ++videoIdToLikes[videoId];  }   void dislike(int videoId) {    if (videoIdToVideo.contains(videoId))      ++videoIdToDislikes[videoId];  }   vector<int> getLikesAndDislikes(int videoId) {    return videoIdToVideo.contains(videoId)               ? vector<int>{videoIdToLikes[videoId],                             videoIdToDislikes[videoId]}               : vector<int>{-1};  }   int getViews(int videoId) {    return videoIdToVideo.contains(videoId) ? videoIdToViews[videoId] : -1;  }  private:  int currVideoId = 0;  priority_queue<int, vector<int>, greater<>> usedIds;  unordered_map<int, string> videoIdToVideo;  unordered_map<int, int> videoIdToViews;  unordered_map<int, int> videoIdToLikes;  unordered_map<int, int> videoIdToDislikes;   int getVideoId() {    if (usedIds.empty())      return currVideoId++;    const int minUsedId = usedIds.top();    usedIds.pop();    return minUsedId;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗