Skip to content

Commit cbe7ac1

Browse files
committed
add 362 java
1 parent e0aa3f5 commit cbe7ac1

File tree

4 files changed

+178
-11
lines changed

4 files changed

+178
-11
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-
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 |
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)
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 || |

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241227: 079,212
1+
20241227: 079,212(todo),362
22
20241222: 369,311
33
20241221: 370
44
20241220: 815,871,593,1109

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
2025-02-20 -> ['079,212']
1+
2025-02-20 -> ['079,212(todo),362']
22
2025-02-15 -> ['369,311']
33
2025-02-14 -> ['370']
44
2025-02-13 -> ['815,871,593,1109']
55
2025-02-07 -> ['560,523']
66
2025-02-01 -> ['304,853,325']
7-
2025-01-30 -> ['079,212']
7+
2025-01-30 -> ['079,212(todo),362']
88
2025-01-26 -> ['370(todo)']
99
2025-01-25 -> ['369,311']
1010
2025-01-24 -> ['370', '34,767']
1111
2025-01-23 -> ['815,871,593,1109']
1212
2025-01-20 -> ['722,380']
1313
2025-01-19 -> ['33,81']
14-
2025-01-17 -> ['079,212', '560,523', '253']
14+
2025-01-17 -> ['079,212(todo),362', '560,523', '253']
1515
2025-01-16 -> ['776,31']
1616
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
1717
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1818
2025-01-12 -> ['369,311']
1919
2025-01-11 -> ['370', '304,853,325', '394']
2020
2025-01-10 -> ['815,871,593,1109', '833,950']
21-
2025-01-09 -> ['079,212']
21+
2025-01-09 -> ['079,212(todo),362']
2222
2025-01-05 -> ['370(todo)']
23-
2025-01-04 -> ['079,212', '369,311', '560,523', '53,210,207']
23+
2025-01-04 -> ['079,212(todo),362', '369,311', '560,523', '53,210,207']
2424
2025-01-03 -> ['370', '34,767', '444']
2525
2025-01-02 -> ['815,871,593,1109', '1188,130,855(again)']
26-
2025-01-01 -> ['079,212']
27-
2024-12-30 -> ['079,212', '369,311', '722,380']
28-
2024-12-29 -> ['079,212', '370', '304,853,325', '33,81']
29-
2024-12-28 -> ['079,212', '815,871,593,1109', '900']
26+
2025-01-01 -> ['079,212(todo),362']
27+
2024-12-30 -> ['079,212(todo),362', '369,311', '722,380']
28+
2024-12-29 -> ['079,212(todo),362', '370', '304,853,325', '33,81']
29+
2024-12-28 -> ['079,212(todo),362', '815,871,593,1109', '900']
3030
2024-12-27 -> ['369,311', '560,523', '253', '26,27', '802,1197,26']
3131
2024-12-26 -> ['370', '776,31']
3232
2024-12-25 -> ['369,311', '815,871,593,1109', '004(todo),34(todo),162(todo),275(todo)']
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package LeetCodeJava.Design;
2+
3+
// https://leetcode.com/problems/design-hit-counter/description/
4+
// https://leetcode.ca/all/362.html
5+
6+
import java.util.ArrayDeque;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
/**
11+
* 362. Design Hit Counter
12+
* Design a hit counter which counts the number of hits received in the past 5 minutes.
13+
*
14+
* 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.
15+
*
16+
* It is possible that several hits arrive roughly at the same time.
17+
*
18+
* Example:
19+
*
20+
* HitCounter counter = new HitCounter();
21+
*
22+
* // hit at timestamp 1.
23+
* counter.hit(1);
24+
*
25+
* // hit at timestamp 2.
26+
* counter.hit(2);
27+
*
28+
* // hit at timestamp 3.
29+
* counter.hit(3);
30+
*
31+
* // get hits at timestamp 4, should return 3.
32+
* counter.getHits(4);
33+
*
34+
* // hit at timestamp 300.
35+
* counter.hit(300);
36+
*
37+
* // get hits at timestamp 300, should return 4.
38+
* counter.getHits(300);
39+
*
40+
* // get hits at timestamp 301, should return 3.
41+
* counter.getHits(301);
42+
* Follow up:
43+
* What if the number of hits per second could be very large? Does your design scale?
44+
*
45+
* Difficulty:
46+
* Medium
47+
* Lock:
48+
* Prime
49+
* Company:
50+
* Adobe Amazon Apple Bloomberg Booking.com Dropbox Google Indeed LinkedIn Microsoft Oracle Pinterest Quip (Salesforce) Snapchat Uber Visa Yahoo Zillow
51+
* Problem Solution
52+
* 362-Design-Hit-Counter
53+
*
54+
*/
55+
public class DesignHitCounter {
56+
57+
// V0
58+
59+
// V1-1
60+
// https://leetcode.ca/2016-11-26-362-Design-Hit-Counter/
61+
public class HitCounter_1_1 {
62+
private ArrayDeque<Integer> queue; // @note: ArrayDeque has no capacity restrictions
63+
64+
// Why is ArrayDeque better than LinkedList?
65+
// If you need add/remove of the both ends, ArrayDeque is significantly better than a linked list
66+
// https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist
67+
68+
/** Initialize your data structure here. */
69+
public HitCounter_1_1() {
70+
queue = new ArrayDeque<Integer>();
71+
}
72+
73+
/** Record a hit.
74+
@param timestamp - The current timestamp (in seconds granularity). */
75+
public void hit(int timestamp) {
76+
queue.offer(timestamp);
77+
}
78+
79+
/** Return the number of hits in the past 5 minutes.
80+
@param timestamp - The current timestamp (in seconds granularity). */
81+
82+
// Time Complexity : O(n)
83+
public int getHits(int timestamp) {
84+
int startTime = timestamp - 300;
85+
86+
// remove all hits over 300 seconds old
87+
while(!queue.isEmpty() && queue.peek() <= startTime) {
88+
queue.poll();
89+
}
90+
return queue.size();
91+
}
92+
}
93+
94+
public class HitCounterFollowUp {
95+
private int[] times;
96+
private int[] hits;
97+
98+
/** Initialize your data structure here. */
99+
public HitCounterFollowUp() {
100+
int[] times = new int[300];
101+
int[] hits = new int[300];
102+
}
103+
104+
/** Record a hit.
105+
@param timestamp - The current timestamp (in seconds granularity). */
106+
public void hit(int timestamp) {
107+
int idx = timestamp % 300;
108+
if (times[idx] != timestamp) {
109+
times[idx] = timestamp; // update with most recent timestamp
110+
hits[idx] = 1;
111+
} else {
112+
++hits[idx];
113+
}
114+
}
115+
116+
/** Return the number of hits in the past 5 minutes.
117+
@param timestamp - The current timestamp (in seconds granularity). */
118+
119+
// Time Complexity : O(n)
120+
public int getHits(int timestamp) {
121+
int res = 0;
122+
for (int i = 0; i < 300; ++i) {
123+
if (timestamp - times[i] < 300) { // values in times[] not ordered at all
124+
res += hits[i];
125+
}
126+
}
127+
return res;
128+
}
129+
}
130+
131+
// V1-2
132+
// https://leetcode.ca/2016-11-26-362-Design-Hit-Counter/
133+
class HitCounter_1_2 {
134+
135+
private Map<Integer, Integer> counter;
136+
137+
/** Initialize your data structure here. */
138+
public HitCounter_1_2() {
139+
counter = new HashMap<>();
140+
}
141+
142+
/**
143+
Record a hit.
144+
@param timestamp - The current timestamp (in seconds granularity).
145+
*/
146+
public void hit(int timestamp) {
147+
counter.put(timestamp, counter.getOrDefault(timestamp, 0) + 1);
148+
}
149+
150+
/**
151+
Return the number of hits in the past 5 minutes.
152+
@param timestamp - The current timestamp (in seconds granularity).
153+
*/
154+
public int getHits(int timestamp) {
155+
int hits = 0;
156+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
157+
if (entry.getKey() + 300 > timestamp) {
158+
hits += entry.getValue();
159+
}
160+
}
161+
return hits;
162+
}
163+
}
164+
165+
166+
// V2
167+
}

0 commit comments

Comments
 (0)