From 064e452303ef0d9533e451915db1d4530b62d019 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 27 Dec 2024 15:58:57 +0800 Subject: [PATCH] update 362 java --- README.md | 2 +- .../LeetCodeJava/Design/DesignHitCounter.java | 38 ++++++++++++++++ .../src/main/java/dev/workspace6.java | 43 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6b3a7af0..24318fa8 100644 --- a/README.md +++ b/README.md @@ -1179,7 +1179,7 @@ 348| [Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/) | [Python](./leetcode_python/Design/design-tic-tac-toe.py) | _O(1)_ | _O(n^2)_ | Medium |matrix, array, 🔒, google, `fb`, `amazon`| AGAIN****** (5) 353| [Design Snake Game](https://leetcode.com/problems/design-snake-game/) |[Python](./leetcode_python/Design/design-snake-game.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Design/DesignSnakeGame.java) | _O(1)_ | _O(s)_ | Medium |good trick, Deque, 🔒, amazon, google, square, m$ | AGAIN*** (1) 355| [Design Twitter](https://leetcode.com/problems/design-twitter/) | [Python](./leetcode_python/Design/design-twitter.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Design/DesignTwitter.java)| _O(klogu)_ | _O(t + f)_ | Medium | good trick, data structure, heapq, defaultdict, `amazon` | OK******** (6) -362| [Design Hit Counter](https://leetcode.com/problems/design-hit-counter/) | [Java](./leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java) | _O(1), amortized_ | _O(k)_ | Medium |🔒| Deque,google | Again (1) (not start) +362| [Design Hit Counter](https://leetcode.com/problems/design-hit-counter/) | [Java](./leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java) | _O(1), amortized_ | _O(k)_ | Medium |🔒| Deque,google | Again (1) 0379| [Design Phone Directory](https://leetcode.com/problems/design-phone-directory/) | [C++](./C++/design-phone-directory.cpp) [Python](./leetcode_python/Design/design-phone-directory.py) | _O(1)_ | _O(n)_ | Medium |🔒| | 380| [Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | [Python](./leetcode_python/Design/insert_delete_get_random_O_1.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Design/InsertDeleteGetRandom0_1.java) | _O(1)_ | _O(n)_| Medium/Hard |set, list, dict, `amazon`,`fb`, google| OK* (5) 0381| [Insert Delete GetRandom O(1) - Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/) | [C++](./C++/insert-delete-getrandom-o1-duplicates-allowed.cpp) [Python](./leetcode_python/Design/insert-delete-getrandom-o1-duplicates-allowed.py) | _O(1)_ | _O(n)_ | Hard || | diff --git a/leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java b/leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java index 706695db..07466e7e 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java @@ -55,6 +55,44 @@ public class DesignHitCounter { // V0 + // TODO: validate + class HitCounter { + + /** counter : {timestamp : cnt } + */ + private Map counter; + + /** Initialize your data structure here. */ + public HitCounter() { + counter = new HashMap<>(); // ?? check + } + + /** + Record a hit. + @param timestamp - The current timestamp (in seconds granularity). + */ + public void hit(int timestamp) { + // counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 1); + int curCnt = counter.getOrDefault(counter,0); + //counter.putIfAbsent(timestamp, curCnt+1); + counter.put(timestamp, curCnt+1); + } + + /** + Return the number of hits in the past 5 minutes. + @param timestamp - The current timestamp (in seconds granularity). + */ + public int getHits(int timestamp) { + int val = 0; + for(Integer key : counter.keySet()){ + if (key >= 0 && key <= timestamp - 60 * 5){ + //val += 1; // ?? + val += counter.get(key); + } + } + return val; + } + } // V1-1 // https://leetcode.ca/2016-11-26-362-Design-Hit-Counter/ diff --git a/leetcode_java/src/main/java/dev/workspace6.java b/leetcode_java/src/main/java/dev/workspace6.java index 6a45d529..bd7b89c7 100644 --- a/leetcode_java/src/main/java/dev/workspace6.java +++ b/leetcode_java/src/main/java/dev/workspace6.java @@ -657,4 +657,47 @@ private boolean canFind(char[][] board, String word, int x, int y, StringBuilder return false; } + + // LC 362 + // https://leetcode.ca/all/362.html + // 3.38 PM - 3.50 PM + class HitCounter { + + /** counter : {timestamp : cnt } ?? + * + */ + private Map counter; + + /** Initialize your data structure here. */ + public HitCounter() { + counter = new HashMap<>(); // ?? check + } + + /** + Record a hit. + @param timestamp - The current timestamp (in seconds granularity). + */ + public void hit(int timestamp) { + // counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 1); + int curCnt = counter.getOrDefault(counter,0); + //counter.putIfAbsent(timestamp, curCnt+1); + counter.put(timestamp, curCnt+1); + } + + /** + Return the number of hits in the past 5 minutes. + @param timestamp - The current timestamp (in seconds granularity). + */ + public int getHits(int timestamp) { + int val = 0; + for(Integer key : counter.keySet()){ + if (key >= 0 && key <= timestamp - 60 * 5){ + //val += 1; // ?? + val += counter.get(key); + } + } + return val; + } + } + }