-
[Day6] 981. Time Based Key-Value Store@StudY/.algorithm 2019. 2. 20. 14:15
981. Time Based Key-Value Store
import java.util.HashMap.*;
class TimeMap {
HashMap<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
/** Initialize your data structure here. */
public TimeMap() {
}
public void set(String key, String value, int timestamp) {
if (!map.containsKey(key)) {
ArrayList<String> list = new ArrayList<String>();
list.add(value);
list.add(timestamp+"");
map.put(key,list);
} else {
map.get(key).add(value);
map.get(key).add(timestamp+"");
}
}
public String get(String key, int timestamp) {
if (map.containsKey(key)) {
for (int i=map.get(key).size()-1; i>=0; i-=2) {
if (Integer.parseInt(map.get(key).get(i)) <= timestamp) return map.get(key).get(i-1);
}
}
return "";
}
}
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap obj = new TimeMap();
* obj.set(key,value,timestamp);
* String param_2 = obj.get(key,timestamp);
*/
Accepted 240 ms 140.7 MB java '@StudY > .algorithm' 카테고리의 다른 글
[Day8/9] 409. Longest Palindrome (0) 2019.02.26 [Day7] 876. Middle of the Linked List / 86. Partition List (LeetCode) (0) 2019.02.22 [Day4] 529. Minesweeper (0) 2019.02.18 [Day3] 108. Convert Sorted Array to Binary Search Tree (0) 2019.02.15 [Day2] 617. Merge Two Binary Trees/ 888. Fair Candy Swap (LeetCode) (0) 2019.02.14 댓글