359. Logger Rate Limiter

Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.
Given a message and a timestamp (in seconds granularity), return true if the message should be printed in the given timestamp, otherwise returns false.
It is possible that several messages arrive roughly at the same time.
Example:

Logger logger = new Logger();

// logging string "foo" at timestamp 1
logger.shouldPrintMessage(1, "foo"); returns true; 

// logging string "bar" at timestamp 2
logger.shouldPrintMessage(2,"bar"); returns true;

// logging string "foo" at timestamp 3
logger.shouldPrintMessage(3,"foo"); returns false;

// logging string "bar" at timestamp 8
logger.shouldPrintMessage(8,"bar"); returns false;

// logging string "foo" at timestamp 10
logger.shouldPrintMessage(10,"foo"); returns false;

// logging string "foo" at timestamp 11
logger.shouldPrintMessage(11,"foo"); returns true;

Solution1:Hashmap

思路: map(message, vali_timestamp)统刮,vali_timestamp是表示 出現(xiàn)過狀態(tài)的最晚時間
Time Complexity: O(N) Space Complexity: O(N)

Solution2:space friendly [design] 小(舊)頂堆 過濾 保持最大的10

思路: map(message, vali_timestamp),vali_timestamp是表示 出現(xiàn)過狀態(tài)的最晚時間
Time Complexity: O() Space Complexity: O(N)

Solution1 Code:

class Logger {
    private Map<String, Integer> vali_map;
    
    public Logger() {
        vali_map = new HashMap<>();;
    }
    
    public boolean shouldPrintMessage(int timestamp, String message) {
        if (timestamp < vali_map.getOrDefault(message, 0))
            return false;
        ok.put(message, timestamp + 10);
        return true;
    }
}

Solution2 Code:

public class Logger {
    class Log {
        int timestamp;
        String message;
        public Log(int timestamp, String message) {
            this.timestamp = timestamp;
            this.message = message;
        }
    }
    
    PriorityQueue<Log> recent_logs;
    Set<String> recent_messages;   
    
    /** Initialize your data structure here. */
    public Logger() {
        recent_logs = new PriorityQueue<Log>(10, new Comparator<Log>() {
            public int compare(Log l1, Log l2) {
                return l1.timestamp - l2.timestamp;
            }
        });
        
        recent_messages = new HashSet<String>();
    }
    
    /** Returns true if the message should be printed in the given timestamp, otherwise returns false.
        If this method returns false, the message will not be printed.
        The timestamp is in seconds granularity. */
    public boolean shouldPrintMessage(int timestamp, String message) {
        while (recent_logs.size() > 0)   {
            Log log = recent_logs.peek();
            // discard the logs older than 10 minutes
            if (timestamp - log.timestamp >= 10) {
                recent_logs.poll();
                recent_messages.remove(log.message);
            } 
            else { 
                break;
            }
        }
        boolean res = !recent_messages.contains(message);
        if (res) {
            recent_logs.add(new Log(timestamp, message));
            recent_messages.add(message);
        }
        return res;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌懂版,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異炬丸,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進店門稠炬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來焕阿,“玉大人,你說我怎么就攤上這事首启∧郝牛” “怎么了?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵毅桃,是天一觀的道長褒纲。 經(jīng)常有香客問我,道長钥飞,這世上最難降的妖魔是什么莺掠? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮读宙,結(jié)果婚禮上彻秆,老公的妹妹穿的比我還像新娘。我一直安慰自己论悴,他們只是感情好掖棉,可當我...
    茶點故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著膀估,像睡著了一般幔亥。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上察纯,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天帕棉,我揣著相機與錄音,去河邊找鬼饼记。 笑死香伴,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的具则。 我是一名探鬼主播即纲,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼博肋!你這毒婦竟也來了低斋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤匪凡,失蹤者是張志新(化名)和其女友劉穎膊畴,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體病游,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡唇跨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片买猖。...
    茶點故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡改橘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出政勃,到底是詐尸還是另有隱情唧龄,我是刑警寧澤奸远,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布既棺,位于F島的核電站,受9級特大地震影響懒叛,放射性物質(zhì)發(fā)生泄漏丸冕。R本人自食惡果不足惜胖烛,卻給世界環(huán)境...
    茶點故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一佩番、第九天 我趴在偏房一處隱蔽的房頂上張望罢杉。 院中可真熱鬧,春花似錦滩租、人聲如沸赋秀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽猎莲。三九已至,卻和暖如春技即,著一層夾襖步出監(jiān)牢的瞬間著洼,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工而叼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留郭脂,地道東北人。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓澈歉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親屿衅。 傳聞我的和親對象是個殘疾皇子埃难,可洞房花燭夜當晚...
    茶點故事閱讀 44,652評論 2 354