zoukankan      html  css  js  c++  java
  • LeetCode "Logger Rate Limiter"

    Queue + hashset

    class Logger {
        typedef pair<string, int> Rec;
        queue<Rec> q;
        unordered_set<string> hs;
    public:
        /** Initialize your data structure here. */
        Logger() {
            
        }
        
        /** Returns true if the message should be printed in the given timestamp, otherwise returns false. The timestamp is in seconds granularity. */
        bool shouldPrintMessage(int timestamp, string message) 
        {
            while(!q.empty() && (timestamp - q.front().second) >= 10)
            {
                hs.erase(q.front().first);
                q.pop();
            }
            
            if(hs.find(message) != hs.end()) return false;
            
            q.push({message, timestamp});
            hs.insert(message);
            
            return true;
        }
    };
  • 相关阅读:
    poj 2443
    codeforces 263D
    codeforces 263C
    uva 11080
    uva 11235
    uva 11748
    STL uva 11991
    (KM) uva 11383
    (树形DP) uva 10859
    codeforces 242C
  • 原文地址:https://www.cnblogs.com/tonix/p/5592683.html
Copyright © 2011-2022 走看看