From 9524759a5151b4ec008bd4844cb9744462330c01 Mon Sep 17 00:00:00 2001 From: joney000 Date: Mon, 18 Mar 2024 19:46:26 +0530 Subject: [PATCH 1/2] fix declaration --- Solution.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Solution.java b/Solution.java index fbd98f7..054e278 100644 --- a/Solution.java +++ b/Solution.java @@ -17,13 +17,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; @@ -39,10 +39,10 @@ void run()throws Exception { int tests = i(); test: for(int testId = 1; testId <= tests; testId++){ - // out.write("Case #"+testId+": "); + // Codejam / Hackercup formatting + // out.write("Case #" + testId + ": "); long ans = 0; out.write(ans + "\n"); - } } From 1a0e8f669256bb9a5a91233f1d8a7bd716c9c8f0 Mon Sep 17 00:00:00 2001 From: joney000 Date: Sat, 21 Sep 2024 22:10:53 +0530 Subject: [PATCH 2/2] add redis add delete stub --- Solution.java | 95 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 3 deletions(-) diff --git a/Solution.java b/Solution.java index 054e278..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{ @@ -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++){ // Codejam / Hackercup formatting // out.write("Case #" + testId + ": "); - long ans = 0; - out.write(ans + "\n"); + 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"); } }