Tuesday, June 11, 2019

LeetCode [635] Design Log Storage System

You are given several logs that 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.
Design a log storage system to implement the following functions:
void Put(int id, string timestamp): Given a log's unique id and timestamp, store the log in your storage system.

int[] Retrieve(String start, String end, String granularity): Return the id of logs whose timestamps are within the range from start to end. Start and end all have the same format as timestamp. However, granularity means the time level for consideration. For example, start = "2017:01:01:23:59:59", end = "2017:01:02:23:59:59", granularity = "Day", it means that we need to find the logs within the range from Jan. 1st 2017 to Jan. 2nd 2017.
Example 1:
put(1, "2017:01:01:23:59:59");
put(2, "2017:01:01:22:59:59");
put(3, "2016:01:01:00:00:00");
retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Year"); // return [1,2,3], because you need to return all logs within 2016 and 2017.
retrieve("2016:01:01:01:01:01","2017:01:01:23:00:00","Hour"); // return [1,2], because you need to return all logs start from 2016:01:01:01 to 2017:01:01:23, where log 3 is left outside the range.
Note:
  1. There will be at most 300 operations of Put or Retrieve.
  2. Year ranges from [2000,2017]. Hour ranges from [00,23].
  3. Output for Retrieve has no order required.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class LogSystem {
    map<string, vector<int>> mp;//time, id
    map<string, int> unit;
public:
    LogSystem() {
        unit["Year"] = 0;
        unit["Month"] = 1;
        unit["Day"] = 2;
        unit["Hour"] = 3;
        unit["Minute"] = 4;
        unit["Second"] = 5;
    }
    
    //t1>t2: 1; t1==t2 0; t1<t2 -1
    int comp(string t1, string t2, string gra){
        stringstream s1(t1), s2(t2);
        for(int i=0; i<=unit[gra]; ++i){
            string v1, v2;
            getline(s1, v1 ,':');
            getline(s2, v2 ,':');
            if(v1>v2) return 1;
            if(v1<v2) return -1;
        }
        return 0;
    }

    void put(int id, string timestamp) {
        mp[timestamp].push_back(id);
    }
    
    vector<int> retrieve(string s, string e, string gra) {
        vector<pair<string, vector<int>>> vec;
        for(auto m:mp){
            vec.push_back(make_pair(m.first, m.second));
        }
        sort(vec.begin(), vec.end());
        vector<int> ret;
        for(auto v:vec){
            if(comp(v.first, s, gra)>=0 && comp(v.first, e, gra)<=0){
                ret.insert(ret.end(), v.second.begin(), v.second.end());
            }
        }
        return ret;
    }
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class LogSystem {
    TreeMap<String, Integer> tMap;
    public int compare(String s1, String s2){
        String[] ss1 = s1.split(":");
        String[] ss2 = s2.split(":");
        for(int i=0; i<6; ++i){
            if(ss1[i].compareTo(ss2[i])!=0) return ss1[i].compareTo(ss2[i]);
        }
        return 0;
    }
    public LogSystem() {
        tMap = new TreeMap<String, Integer>((a, b) ->  compare(a,b));
    }
    
    public void put(int id, String timestamp) {
        tMap.put(timestamp, id);
    }
    
    String processTime(String str, String granularity, boolean isStart){
        int index = -1;
        if(granularity.equals("Year")) index = 0;
        if(granularity.equals("Month")) index = 1;
        if(granularity.equals("Day")) index = 2;
        if(granularity.equals("Hour")) index = 3;
        if(granularity.equals("Minute")) index = 4;
        if(granularity.equals("Second")) index = 5;
        String[] strs = str.split(":");
        for(int i=index+1; i<6; ++i) strs[i] = isStart?"00":"59";
        StringBuilder sb = new StringBuilder();
        for(String s : strs){
            if(sb.length()>0) sb.append(":");
            sb.append(s);
        }
        return sb.toString();
    }

    public List<Integer> retrieve(String start, String end, String granularity) {
        
        List<Integer> list = new ArrayList<>();
        if(compare(start, end)>0) return list;
        String l = tMap.ceilingKey(processTime(start, granularity, true));
        String r = tMap.floorKey(processTime(end, granularity, false));
        if(l==null || r==null || compare(l, r)>0) return list;
        for(int id : tMap.subMap(l, true, r, true).values()){
            list.add(id);
        }
        return list;
    }
}

No comments:

Post a Comment