Interview 150

Time Based Key-Value Store

Problem
LC 981
File
interview150_LC981TimeBasedKeyValueStore.java
Path
pkg5leetcode/interview150/interview150_LC981TimeBasedKeyValueStore.java
Package
pkg5leetcode.interview150
Command
java pkg5leetcode/interview150/interview150_LC981TimeBasedKeyValueStore.java

LeetCode solutions

There is no in-browser runner. This is the file from the curriculum, unchanged.

pkg5leetcode/interview150/interview150_LC981TimeBasedKeyValueStore.java
1package pkg5leetcode.interview150;2 3/** LC 981 Time Based Key-Value Store */4import java.util.*;5 6public class interview150_LC981TimeBasedKeyValueStore {7  static class TimeMap {8    Map<String, TreeMap<Integer,String>> map = new HashMap<>();9    void set(String key, String value, int ts) {10      map.computeIfAbsent(key, k -> new TreeMap<>()).put(ts, value);11    }12    String get(String key, int ts) {13      var tm = map.get(key);14      if (tm == null) return "";15      var e = tm.floorEntry(ts);16      return e == null ? "" : e.getValue();17    }18  }19 20  public static void main(String[] args) {21    TimeMap tm = new TimeMap();22    tm.set("foo","bar",1);23    System.out.println(tm.get("foo",1));24    System.out.println(tm.get("foo",3));25  }26}