LeetCode Design Log Storage System
原题链接在这里: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:
- There will be at most 300 operations of Put or Retrieve.
- Year ranges from [2000,2017]. Hour ranges from [00,23].
- 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的更多相关文章
- [LeetCode] Design Log Storage System 设计日志存储系统
You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...
- Design Log Storage System
You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...
- [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 ...
- [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统
Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...
- [LeetCode] Design In-Memory File System 设计内存文件系统
Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...
- Bigtable: A Distributed Storage System for Structured Data
https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf Abstr ...
- Storage System and File System Courses
I researched a lot about storage system classes given at good universities this year. This had two r ...
- 1.1 Introduction中 Kafka as a Storage System官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Storage System kafka作为一个存储系统 An ...
- Blockstack: A Global Naming and Storage System Secured by Blockchains
作者:Muneeb Ali, Jude Nelson, Ryan Shea, and Michael Freedman Blockstack Labs and Princeton University ...
随机推荐
- MySQL行级锁和表级锁
锁定用于确保事务完整性和数据库一致性. 锁定可以防止用户读取其他用户正在更改的数据,并防止多个用户同时更改相同的数据. 如果不使用锁定,数据库中的数据可能在逻辑上变得不正确,而针对这些数据进行查询可能 ...
- discuz对PHP7不支持mysql的兼容性处理
PHP7 废除了 ”mysql.dll” ,推荐使用 mysqli 或者 pdo_mysql,discuz对原生mysql函数做了如下处理,通过mysqli代替原mysql函数 http://blog ...
- 配置nginx,Tomcat日志记录请求耗时
由于公司的业务比较特殊,对速度比较在意,客户最近反应我们的平台时间比较久,处理一个请求十秒左右才返回,领导要求找出原因,我想让nginx日志记录请求处理用了多长时间,后端处理用了多长时间,总共用了多长 ...
- Java控制语句——分支、循环、跳转
分支语句(if语句,switch语句): 循环语句(for,while,do...while); 跳转语句(break,continue,return): 分支语句(if语句,switch语句) if ...
- JMS-activMq与spring进行整合
对JMS做了一个简要介绍之后,接下来就讲一下Spring整合JMS的具体过程.JMS只是一个标准,真正在使用它的时候我们需要有它的具体实现,这里我们就使用Apache的activeMQ来作为它的实现 ...
- html checkbox多选框语法与结构
<input name="Fruit" type="checkbox" value="" /> 用法用例 <foreach ...
- codeforces781A Andryusha and Colored Balloons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Windows下使用MinGW在命令行编译运行C++程序
之前学习C语言的时候都是用IDE类似CodeBlocks的工具写完直接编译运行的,今天突然心血来潮,自己下一个编译器,在命令行下,编译运行C++程序,了解一下编译过程. 一.安装编译器 首先你需要下载 ...
- spirng: srping mvc配置(访问路径配置)搭建SpringMVC——最小化配置
搭建SpringMVC——最小化配置 最开始接触网页的时候,是纯的html/css页面,那个时候还是用Dreamweaver来绘制页面. 随着网站开发的深入,开始学习servlet开发,记得最痛苦的就 ...
- 来自lombok的注解(解决idea中的找不到get,set方法,找不到log的问题)
今天看代码,发现idea报错,仔细一看调用的get,set方法bean中都没有,但是运行起来却没有问题,这个让我很疑惑.后来发现在类上有一个以前没见过的注解@Data,大概就是因为有他的原因.这个注解 ...