Advertisement
thewitchking

Untitled

Jul 6th, 2025
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <unordered_set>
  4.  
  5. class VideoHistory {
  6. private:
  7.     std::list<int> history; // Maintains order (most recent first)
  8.     std::unordered_set<int> seen; // For quick lookup to avoid duplicates
  9.     const size_t maxSize = 3;
  10.  
  11. public:
  12.     void watchVideo(int videoId) {
  13.         // If already in history, remove it first
  14.         if (seen.find(videoId) != seen.end()) {
  15.             history.remove(videoId);
  16.         } else if (history.size() == maxSize) {
  17.             // If full, remove the least recently watched (last element)
  18.             int last = history.back();
  19.             history.pop_back();
  20.             seen.erase(last);
  21.         }
  22.  
  23.         // Add to front as most recent
  24.         history.push_front(videoId);
  25.         seen.insert(videoId);
  26.     }
  27.  
  28.     void printHistory() const {
  29.         std::cout << "Video History (most recent first): ";
  30.         for (int id : history) {
  31.             std::cout << id << " ";
  32.         }
  33.         std::cout << std::endl;
  34.     }
  35. };
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement