diff --git a/Solution.java b/Solution.java index fbd98f7..6c438e4 100644 --- a/Solution.java +++ b/Solution.java @@ -9,6 +9,68 @@ * Platform : Codeforces/Codejam * Ref : N/A */ +class Entry { + String value; + long expiredAt; + + public Entry(String value, long expiredAt){ + this.value = value; + this.expiredAt = expiredAt; + } + + @Override + public String toString(){ + return "Entry{ value:" + value + ", expiredAt: " + expiredAt + "}"; + } +} + +class InMemoryDB { + + HashMap> cache = new HashMap<>(); + + InMemoryDB(){ + } + + void addEntry(long timestamp, String key, String field, String value, long ttl){ + long expiredAt = timestamp + ttl; + var entries = new HashMap(); + if (cache.containsKey(key)) { + entries = (HashMap)cache.get(key); + } + entries.put(field, new Entry(value, expiredAt)); + cache.put(key, entries); + } + + void evictIfExpired(long timestamp, String key, String field) { + var entries = (HashMap)cache.get(key); + Entry entry = (Entry) entries.get(field); + if (entry.expiredAt <= timestamp){ + entries.remove(field); + cache.put(key, entries); + } + } + + boolean deleteEntry(long timestamp, String key, String field){ + if (cache.containsKey(key)) { + var entries = (HashMap)cache.get(key); + evictIfExpired(timestamp, key, field); + if(entries.containsKey(field)){ + entries.remove(field); + cache.put(key, entries); + return true; + }else { + return false; + } + } + return false; + } + + List scan(long timestamp, String key){ + // similarly O(N) iterate and evictIfExpired and return values + return null; + } + +} public class Solution{ @@ -17,13 +79,13 @@ public class Solution{ private FastReader in ; private PrintWriter out ; private final String PROBLEM_ID = "1039-A"; - private final long MOD = 1000000000+7; + private final long MOD = (long)1e9 + 7; private final int INF = Integer.MAX_VALUE; private final long INF_L = Long.MAX_VALUE / 2; public Solution(){} + public Solution(boolean stdIO)throws FileNotFoundException{ - // stdIO = false; if(stdIO){ inputStream = System.in; outputStream = System.out; @@ -34,15 +96,42 @@ public Solution(boolean stdIO)throws FileNotFoundException{ in = new FastReader(inputStream); out = new PrintWriter(outputStream); } - void run()throws Exception { int tests = i(); test: for(int testId = 1; testId <= tests; testId++){ - // out.write("Case #"+testId+": "); - long ans = 0; - out.write(ans + "\n"); - + // Codejam / Hackercup formatting + // out.write("Case #" + testId + ": "); + Random rng = new Random(); + InMemoryDB database = new InMemoryDB(); + + for(int operations = 1; operations <= 1000; operations++){ + long timestamp = operations * operations; + switch(rng.nextInt(4)){ + // add + case 0: { + long ttl = rng.nextLong((int)1e9); + String key = "key_" + rng.nextInt(100);// random key + String field = "field_" + rng.nextInt(100);// random field + String value = "value_" + rng.nextInt(100);// random field + + database.addEntry(timestamp, key, field, value, ttl); + break; + } + // delete + case 1: { + String key = "key_" + rng.nextInt(100);// random key + String field = "field_" + rng.nextInt(100);// random field + database.deleteEntry(timestamp, key, field); + break; + } + // TODO: Scan disctionary + default: { + break; + } + } + } + // out.write(ans + "\n"); } }