From cbe7ac1ad5ca2a859123c2d5ccd5fb4219bd263d Mon Sep 17 00:00:00 2001 From: yennanliu Date: Fri, 27 Dec 2024 15:33:09 +0800 Subject: [PATCH] add 362 java --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +- .../LeetCodeJava/Design/DesignHitCounter.java | 167 ++++++++++++++++++ 4 files changed, 178 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java diff --git a/README.md b/README.md index b52e1897..6b3a7af0 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) -0362| [Design Hit Counter](https://leetcode.com/problems/design-hit-counter/) | [C++](./C++/design-hit-counter.cpp) [Python](./leetcode_python/Design/design-hit-counter.py) | _O(1), amortized_ | _O(k)_ | Medium |🔒| Deque | +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) 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/data/progress.txt b/data/progress.txt index 80ffec69..52514443 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20241227: 079,212 +20241227: 079,212(todo),362 20241222: 369,311 20241221: 370 20241220: 815,871,593,1109 diff --git a/data/to_review.txt b/data/to_review.txt index 37a912c9..4fee1e37 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,32 +1,32 @@ -2025-02-20 -> ['079,212'] +2025-02-20 -> ['079,212(todo),362'] 2025-02-15 -> ['369,311'] 2025-02-14 -> ['370'] 2025-02-13 -> ['815,871,593,1109'] 2025-02-07 -> ['560,523'] 2025-02-01 -> ['304,853,325'] -2025-01-30 -> ['079,212'] +2025-01-30 -> ['079,212(todo),362'] 2025-01-26 -> ['370(todo)'] 2025-01-25 -> ['369,311'] 2025-01-24 -> ['370', '34,767'] 2025-01-23 -> ['815,871,593,1109'] 2025-01-20 -> ['722,380'] 2025-01-19 -> ['33,81'] -2025-01-17 -> ['079,212', '560,523', '253'] +2025-01-17 -> ['079,212(todo),362', '560,523', '253'] 2025-01-16 -> ['776,31'] 2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)'] 2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)'] 2025-01-12 -> ['369,311'] 2025-01-11 -> ['370', '304,853,325', '394'] 2025-01-10 -> ['815,871,593,1109', '833,950'] -2025-01-09 -> ['079,212'] +2025-01-09 -> ['079,212(todo),362'] 2025-01-05 -> ['370(todo)'] -2025-01-04 -> ['079,212', '369,311', '560,523', '53,210,207'] +2025-01-04 -> ['079,212(todo),362', '369,311', '560,523', '53,210,207'] 2025-01-03 -> ['370', '34,767', '444'] 2025-01-02 -> ['815,871,593,1109', '1188,130,855(again)'] -2025-01-01 -> ['079,212'] -2024-12-30 -> ['079,212', '369,311', '722,380'] -2024-12-29 -> ['079,212', '370', '304,853,325', '33,81'] -2024-12-28 -> ['079,212', '815,871,593,1109', '900'] +2025-01-01 -> ['079,212(todo),362'] +2024-12-30 -> ['079,212(todo),362', '369,311', '722,380'] +2024-12-29 -> ['079,212(todo),362', '370', '304,853,325', '33,81'] +2024-12-28 -> ['079,212(todo),362', '815,871,593,1109', '900'] 2024-12-27 -> ['369,311', '560,523', '253', '26,27', '802,1197,26'] 2024-12-26 -> ['370', '776,31'] 2024-12-25 -> ['369,311', '815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java b/leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java new file mode 100644 index 00000000..706695db --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java @@ -0,0 +1,167 @@ +package LeetCodeJava.Design; + +// https://leetcode.com/problems/design-hit-counter/description/ +// https://leetcode.ca/all/362.html + +import java.util.ArrayDeque; +import java.util.HashMap; +import java.util.Map; + +/** + * 362. Design Hit Counter + * Design a hit counter which counts the number of hits received in the past 5 minutes. + * + * Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing). You may assume that the earliest timestamp starts at 1. + * + * It is possible that several hits arrive roughly at the same time. + * + * Example: + * + * HitCounter counter = new HitCounter(); + * + * // hit at timestamp 1. + * counter.hit(1); + * + * // hit at timestamp 2. + * counter.hit(2); + * + * // hit at timestamp 3. + * counter.hit(3); + * + * // get hits at timestamp 4, should return 3. + * counter.getHits(4); + * + * // hit at timestamp 300. + * counter.hit(300); + * + * // get hits at timestamp 300, should return 4. + * counter.getHits(300); + * + * // get hits at timestamp 301, should return 3. + * counter.getHits(301); + * Follow up: + * What if the number of hits per second could be very large? Does your design scale? + * + * Difficulty: + * Medium + * Lock: + * Prime + * Company: + * Adobe Amazon Apple Bloomberg Booking.com Dropbox Google Indeed LinkedIn Microsoft Oracle Pinterest Quip (Salesforce) Snapchat Uber Visa Yahoo Zillow + * Problem Solution + * 362-Design-Hit-Counter + * + */ +public class DesignHitCounter { + + // V0 + + // V1-1 + // https://leetcode.ca/2016-11-26-362-Design-Hit-Counter/ + public class HitCounter_1_1 { + private ArrayDeque queue; // @note: ArrayDeque has no capacity restrictions + + // Why is ArrayDeque better than LinkedList? + // If you need add/remove of the both ends, ArrayDeque is significantly better than a linked list + // https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist + + /** Initialize your data structure here. */ + public HitCounter_1_1() { + queue = new ArrayDeque(); + } + + /** Record a hit. + @param timestamp - The current timestamp (in seconds granularity). */ + public void hit(int timestamp) { + queue.offer(timestamp); + } + + /** Return the number of hits in the past 5 minutes. + @param timestamp - The current timestamp (in seconds granularity). */ + + // Time Complexity : O(n) + public int getHits(int timestamp) { + int startTime = timestamp - 300; + + // remove all hits over 300 seconds old + while(!queue.isEmpty() && queue.peek() <= startTime) { + queue.poll(); + } + return queue.size(); + } + } + + public class HitCounterFollowUp { + private int[] times; + private int[] hits; + + /** Initialize your data structure here. */ + public HitCounterFollowUp() { + int[] times = new int[300]; + int[] hits = new int[300]; + } + + /** Record a hit. + @param timestamp - The current timestamp (in seconds granularity). */ + public void hit(int timestamp) { + int idx = timestamp % 300; + if (times[idx] != timestamp) { + times[idx] = timestamp; // update with most recent timestamp + hits[idx] = 1; + } else { + ++hits[idx]; + } + } + + /** Return the number of hits in the past 5 minutes. + @param timestamp - The current timestamp (in seconds granularity). */ + + // Time Complexity : O(n) + public int getHits(int timestamp) { + int res = 0; + for (int i = 0; i < 300; ++i) { + if (timestamp - times[i] < 300) { // values in times[] not ordered at all + res += hits[i]; + } + } + return res; + } + } + + // V1-2 + // https://leetcode.ca/2016-11-26-362-Design-Hit-Counter/ + class HitCounter_1_2 { + + private Map counter; + + /** Initialize your data structure here. */ + public HitCounter_1_2() { + counter = new HashMap<>(); + } + + /** + Record a hit. + @param timestamp - The current timestamp (in seconds granularity). + */ + public void hit(int timestamp) { + counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 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 hits = 0; + for (Map.Entry entry : counter.entrySet()) { + if (entry.getKey() + 300 > timestamp) { + hits += entry.getValue(); + } + } + return hits; + } + } + + + // V2 +}