Advertisement
Guest User

Untitled

a guest
Nov 9th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. class TimeMap {
  2.  
  3. Map<String,Map<Integer,String>> map;
  4.  
  5. /** Initialize your data structure here. */
  6. public TimeMap( ) {
  7. this.map= new HashMap<>();
  8. }
  9.  
  10. public void set(String key, String value, int timestamp) {
  11. map.putIfAbsent(key,new HashMap<Integer,String>());
  12. Map<Integer,String> internalMap= map.get(key);
  13. internalMap.put(timestamp,value);
  14.  
  15. }
  16.  
  17. public String get(String key, int timestamp) {
  18.  
  19.  
  20. if(!map.containsKey(key)){
  21. return "";
  22. }
  23. Map<Integer,String> internalMap = map.get(key);
  24. while(timestamp > 0 && !internalMap.containsKey(timestamp)){
  25. timestamp--;
  26. }
  27. if(timestamp==0){
  28. return "";
  29. }else{
  30. return internalMap.get(timestamp);
  31. }
  32.  
  33.  
  34. }
  35. }
  36. /**
  37. * Your TimeMap object will be instantiated and called as such:
  38. * TimeMap obj = new TimeMap();
  39. * obj.set(key,value,timestamp);
  40. * String param_2 = obj.get(key,timestamp);
  41. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement