Skip to content

Commit 064e452

Browse files
committed
update 362 java
1 parent cbe7ac1 commit 064e452

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@
11791179
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)
11801180
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)
11811181
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)
1182-
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)
1182+
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)
11831183
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 |🔒| |
11841184
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)
11851185
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 || |

leetcode_java/src/main/java/LeetCodeJava/Design/DesignHitCounter.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,44 @@
5555
public class DesignHitCounter {
5656

5757
// V0
58+
// TODO: validate
59+
class HitCounter {
60+
61+
/** counter : {timestamp : cnt }
62+
*/
63+
private Map<Integer, Integer> counter;
64+
65+
/** Initialize your data structure here. */
66+
public HitCounter() {
67+
counter = new HashMap<>(); // ?? check
68+
}
69+
70+
/**
71+
Record a hit.
72+
@param timestamp - The current timestamp (in seconds granularity).
73+
*/
74+
public void hit(int timestamp) {
75+
// counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 1);
76+
int curCnt = counter.getOrDefault(counter,0);
77+
//counter.putIfAbsent(timestamp, curCnt+1);
78+
counter.put(timestamp, curCnt+1);
79+
}
80+
81+
/**
82+
Return the number of hits in the past 5 minutes.
83+
@param timestamp - The current timestamp (in seconds granularity).
84+
*/
85+
public int getHits(int timestamp) {
86+
int val = 0;
87+
for(Integer key : counter.keySet()){
88+
if (key >= 0 && key <= timestamp - 60 * 5){
89+
//val += 1; // ??
90+
val += counter.get(key);
91+
}
92+
}
93+
return val;
94+
}
95+
}
5896

5997
// V1-1
6098
// https://leetcode.ca/2016-11-26-362-Design-Hit-Counter/

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,4 +657,47 @@ private boolean canFind(char[][] board, String word, int x, int y, StringBuilder
657657
return false;
658658
}
659659

660+
661+
// LC 362
662+
// https://leetcode.ca/all/362.html
663+
// 3.38 PM - 3.50 PM
664+
class HitCounter {
665+
666+
/** counter : {timestamp : cnt } ??
667+
*
668+
*/
669+
private Map<Integer, Integer> counter;
670+
671+
/** Initialize your data structure here. */
672+
public HitCounter() {
673+
counter = new HashMap<>(); // ?? check
674+
}
675+
676+
/**
677+
Record a hit.
678+
@param timestamp - The current timestamp (in seconds granularity).
679+
*/
680+
public void hit(int timestamp) {
681+
// counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 1);
682+
int curCnt = counter.getOrDefault(counter,0);
683+
//counter.putIfAbsent(timestamp, curCnt+1);
684+
counter.put(timestamp, curCnt+1);
685+
}
686+
687+
/**
688+
Return the number of hits in the past 5 minutes.
689+
@param timestamp - The current timestamp (in seconds granularity).
690+
*/
691+
public int getHits(int timestamp) {
692+
int val = 0;
693+
for(Integer key : counter.keySet()){
694+
if (key >= 0 && key <= timestamp - 60 * 5){
695+
//val += 1; // ??
696+
val += counter.get(key);
697+
}
698+
}
699+
return val;
700+
}
701+
}
702+
660703
}

0 commit comments

Comments
 (0)