Skip to content

Commit

Permalink
update 362 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 27, 2024
1 parent cbe7ac1 commit 064e452
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 || |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,44 @@
public class DesignHitCounter {

// V0
// TODO: validate
class HitCounter {

/** counter : {timestamp : cnt }
*/
private Map<Integer, Integer> 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/
Expand Down
43 changes: 43 additions & 0 deletions leetcode_java/src/main/java/dev/workspace6.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Integer> 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;
}
}

}

0 comments on commit 064e452

Please sign in to comment.