Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <unordered_set>
- class VideoHistory {
- private:
- std::list<int> history; // Maintains order (most recent first)
- std::unordered_set<int> seen; // For quick lookup to avoid duplicates
- const size_t maxSize = 3;
- public:
- void watchVideo(int videoId) {
- // If already in history, remove it first
- if (seen.find(videoId) != seen.end()) {
- history.remove(videoId);
- } else if (history.size() == maxSize) {
- // If full, remove the least recently watched (last element)
- int last = history.back();
- history.pop_back();
- seen.erase(last);
- }
- // Add to front as most recent
- history.push_front(videoId);
- seen.insert(videoId);
- }
- void printHistory() const {
- std::cout << "Video History (most recent first): ";
- for (int id : history) {
- std::cout << id << " ";
- }
- std::cout << std::endl;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement