Design Log Storage System

You are given several logs, where each log contains a unique ID and timestamp. Timestamp is a string that has the following format: Year:Month:Day:Hour:Minute:Second, for example, 2017:01:01:23:59:59. All domains are zero-padded decimal numbers.

Implement the LogSystem class:

  • LogSystem() Initializes the LogSystem object.
  • void put(int id, string timestamp) Stores the given log (id, timestamp) in your storage system.
  • int[] retrieve(string start, string end, string granularity) Returns the IDs of the logs whose timestamps are within the range from start to end inclusive. start and end all have the same format as timestamp, and granularity means how precise the range should be (i.e. to the exact DayMinute, etc.). For example, start = "2017:01:01:23:59:59"end = "2017:01:02:23:59:59", and granularity = "Day" means that we need to find the logs within the inclusive range from Jan. 1st 2017 to Jan. 2nd 2017, and the HourMinute, and Second for each log entry can be ignored.

Example 1:

Input
["LogSystem", "put", "put", "put", "retrieve", "retrieve"]
[[], [1, "2017:01:01:23:59:59"], [2, "2017:01:01:22:59:59"], [3, "2016:01:01:00:00:00"], ["2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year"], ["2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour"]]
Output
[null, null, null, null, [3, 2, 1], [2, 1]]

Explanation
LogSystem logSystem = new LogSystem();
logSystem.put(1, "2017:01:01:23:59:59");
logSystem.put(2, "2017:01:01:22:59:59");
logSystem.put(3, "2016:01:01:00:00:00");

// return [3,2,1], because you need to return all logs between 2016 and 2017.
logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year");

// return [2,1], because you need to return all logs between Jan. 1, 2016 01:XX:XX and Jan. 1, 2017 23:XX:XX.
// Log 3 is not returned because Jan. 1, 2016 00:00:00 comes before the start of the range.
logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour");

思路:核心思想就是 time -> key, value -> list of id; 用treemap自动排序time,然后取submap,submap是通过start granularity前面的 + 后面标准的,来取的,我觉得这个题的考点就是在于怎么取submap;

class LogSystem {
    private String min, max;
    private TreeMap<String, LinkedList<Integer>> treemap;
    private HashMap<String, Integer> indexMap;
    public LogSystem() {
        // Year:Month:Day:Hour:Minute:Second
        this.min = "2000:01:01:00:00:00";
        this.max = "2017:12:31:23:59:59";
        this.indexMap = new HashMap<>();
        indexMap.put("Year", 4);
        indexMap.put("Month", 7);
        indexMap.put("Day", 10);
        indexMap.put("Hour", 13);
        indexMap.put("Minute", 16);
        indexMap.put("Second", 19);
        this.treemap = new TreeMap<>();
    }
    
    public void put(int id, String timestamp) {
        treemap.putIfAbsent(timestamp, new LinkedList<>());
        treemap.get(timestamp).add(id);
    }
    
    public List<Integer> retrieve(String start, String end, String granularity) {
        int index = indexMap.get(granularity);
        // 建成新的start和end;
        String s = start.substring(0, index) + min.substring(index);
        String e = end.substring(0, index) + max.substring(index);
        Map<String, LinkedList<Integer>> targetMap = treemap.subMap(s, true, e, true);
        List<Integer> res = new LinkedList<Integer>();
        for(String key: targetMap.keySet()) {
            res.addAll(targetMap.get(key));
        }
        return res;
    }
}

/**
 * Your LogSystem object will be instantiated and called as such:
 * LogSystem obj = new LogSystem();
 * obj.put(id,timestamp);
 * List<Integer> param_2 = obj.retrieve(start,end,granularity);
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值