(G) Design / OOD 类算法题
public class Logger {
HashMap<String, Integer> map;
/** Initialize your data structure here. */
public Logger() {
map = new HashMap<String, Integer>();
}
/** 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) {
boolean rst = false;
if(!map.containsKey(message) || timestamp - map.get(message) >= 10) rst = true;
if(rst) map.put(message, timestamp);
return rst;
}
}需要注意的细节:
用了另外一个 deque 存 body index 速度就快多了超过 54%, 但是没什么大改动就不贴代码了。另外一个小改动是,其实用不着存 score 变量,因为 score = 蛇长度 - 1
这种做法是靠均摊复杂度平摊 cost 的典型例子。
这题的另一种比较省空间的做法是用 BitSet,就看对 API 的熟悉程度了。
(G) Design Battleship Game
(G) Design Elevator
(A) Design Parking lot
(LinkedIn) Data Stream as Disjoint Intervals
Last updated