原题链接在这里:https://leetcode.com/problems/design-log-storage-system/description/

题目:

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.

题解:

Inorder to retireve range of timestamp. It needs a linear data structure to store log based on timestamp. It could be list or array, but every time, it needs to retrieve everything again.

Or we could use TreeMap. Thus timestamp is sorted.

When we have the start and end time, revise a little bit from the granularity index.

Use TreeMap subMap to get sub map range from [start, end] timestamp.

And add values to res.

Time Complexity: put, O(logn). n = current count of log. retrieve(logn + m). m = count of logs between start and end.

Space: O(n).

AC Java:

 class LogSystem {
private TreeMap<String, HashSet<Integer>> treeMap;
private HashMap<String, Integer> map;
private String min = "2000:01:01:00:00:00";
private String max = "2017:12:31:23:59:59"; public LogSystem() {
this.treeMap = new TreeMap<>();
this.map = new HashMap<>();
map.put("Year", 4);
map.put("Month", 7);
map.put("Day", 10);
map.put("Hour", 13);
map.put("Minute", 16);
map.put("Second", 19);
} public void put(int id, String timestamp) {
treeMap.putIfAbsent(timestamp, new HashSet<>());
treeMap.get(timestamp).add(id);
} public List<Integer> retrieve(String s, String e, String gra) {
int index = map.get(gra);
String start = s.substring(0, index) + min.substring(index);
String end = e.substring(0, index) + max.substring(index); Map<String, HashSet<Integer>> subMap = treeMap.subMap(start, true, end, true);
List<Integer> res = new ArrayList<>();
for(Map.Entry<String, HashSet<Integer>> entry : subMap.entrySet()){
res.addAll(entry.getValue());
} 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(s,e,gra);
*/

用list直接存log. 根据granularity取出timestamp的substring直接比较.

Time Complexity: put, O(1). retrieve, O(logs.size()).

Space: O(logs.size()).

AC Java:

 class LogSystem {
List<String []> logs;
List<String> units = Arrays.asList("Year", "Month", "Day", "Hour", "Minute", "Second");
int [] indices = new int[]{4,7,10,13,16,19}; public LogSystem() {
logs = new LinkedList<String []>();
} public void put(int id, String timestamp) {
logs.add(new String[]{Integer.toString(id), timestamp});
} public List<Integer> retrieve(String s, String e, String gra) {
List<Integer> res = new ArrayList<Integer>();
int ind = indices[units.indexOf(gra)]; String sSub = s.substring(0, ind);
String eSub = e.substring(0, ind); for(String [] log : logs){
String sub = log[1].substring(0, ind);
if(sub.compareTo(sSub)>=0 && sub.compareTo(eSub)<=0){
res.add(Integer.valueOf(log[0]));
}
}
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(s,e,gra);
*/

LeetCode Design Log Storage System的更多相关文章

  1. [LeetCode] Design Log Storage System 设计日志存储系统

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

  2. Design Log Storage System

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

  3. [leetcode-635-Design Log Storage System]

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

  4. [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  5. [LeetCode] Design In-Memory File System 设计内存文件系统

    Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...

  6. Bigtable: A Distributed Storage System for Structured Data

    https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf Abstr ...

  7. Storage System and File System Courses

    I researched a lot about storage system classes given at good universities this year. This had two r ...

  8. 1.1 Introduction中 Kafka as a Storage System官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Storage System kafka作为一个存储系统 An ...

  9. Blockstack: A Global Naming and Storage System Secured by Blockchains

    作者:Muneeb Ali, Jude Nelson, Ryan Shea, and Michael Freedman Blockstack Labs and Princeton University ...

随机推荐

  1. Apache 静态缓存配置

    静态文件缓存 静态缓存在客户端下进行缓存,可以设置缓存文件类型与缓存时间,提升客户端访问站点速度. 语法格式 ExpiresByType type/encoding “<base> [pl ...

  2. 深入理解PHP之:Nginx 与 FPM 的工作机制

    网络上有很多关于如何配置 Nginx + FPM 的文章,但它们更多从操作的角度出发,告诉我们怎么做,但却没有告诉我们为什么要这么做,本文从 Nginx 与 FPM 的工作机制出发,探讨配置背后的原理 ...

  3. C++类初始化列表

    转自:https://www.cnblogs.com/BlueTzar/articles/1223169.html 构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟 ...

  4. 砝码称重V2

    总时间限制:  1000ms 内存限制:  65536kB 描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=100,000),要求:计算用这些砝码能称出的不同重量的个数 ...

  5. 如何查看eclipse、mysql的版本 - 原创

    Eclipse 1)如果实在官网下载的,看压缩包名字就可以看出来,只带有win32字样的是32位,带有win32-x86_64字样的是64位的. 2)找到eclipse安装目录的eclipse.ini ...

  6. Nginx配置端口访问的网站

    server { listen 80; #listen [::]:80 default_server ipv6only=on; server_name www.website.com; index i ...

  7. apue.3e源码下载及编译

    下载地址:http://www.apuebook.com/code3e.html 编译方法:http://blog.sina.com.cn/s/blog_94977c890102vdms.html

  8. JavaScript tips —— target与currentTarget的区别

    定义 以下是红宝书的描述 属性/方法 类型 读/写 说明 currentTarget Element 只读 其事件处理程序当前正在处理事件的那个元素 target Element 只读 事件的目标 M ...

  9. 使用springmvc时报错JSPs only permit GET POST or HEAD

    两个地方需要注意:第一处在web.xml文件中不要忘记配置 <filter> <filter-name>HiddenHttpMethodFilter</filter-na ...

  10. 浅谈 session 会话的原理

    先谈 cookie 网络传输基于的Http协议,是无状态的协议,即每次连接断开后再去连接,服务器是无法判断此次连接的客户端是谁. 如果每次数据传输都需要进行连接和断开,那造成的开销是很巨大的. 为了解 ...