Skip to content

Commit f39be36

Browse files
committed
add 380 java
1 parent fa1ac01 commit f39be36

File tree

5 files changed

+207
-11
lines changed

5 files changed

+207
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@
11801180
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)
11811181
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 |
11821182
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 |🔒| |
1183-
0380| [Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | [Python](./leetcode_python/Design/insert_delete_get_random_O_1.py) | _O(1)_ | _O(n)_| Medium/Hard |set, list, dict, `amazon`,`fb`| OK* (5)
1183+
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)
11841184
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 || |
11851185
432| [All O\`one Data Structure](https://leetcode.com/problems/all-oone-data-structure/) | [C++](./C++/all-oone-data-structure.cpp) [Python](./leetcode_python/Design/all-oone-data-structure.py) | _O(1)_ | _O(n)_| Hard || |
11861186
460| [LFU Cache](https://leetcode.com/problems/lfu-cache/) | [Python](./leetcode_python/Design/lfu_cache.py) | _O(1)_ | _O(k)_ | Hard | Least Frequently Used (LFU) cache, complex, `amazon`| AGAIN*** (2) (not start)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241126: 722
1+
20241126: 722,380
22
20241125: 33,81
33
20241124: 153
44
20241123: 253

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2025-01-20 -> ['722']
1+
2025-01-20 -> ['722,380']
22
2025-01-19 -> ['33,81']
33
2025-01-17 -> ['253']
44
2025-01-16 -> ['776,31']
@@ -9,7 +9,7 @@
99
2025-01-04 -> ['53,210,207']
1010
2025-01-03 -> ['444']
1111
2025-01-02 -> ['1188,130,855(again)']
12-
2024-12-30 -> ['722']
12+
2024-12-30 -> ['722,380']
1313
2024-12-29 -> ['33,81']
1414
2024-12-28 -> ['900']
1515
2024-12-27 -> ['253', '26,27', '802,1197,26']
@@ -19,24 +19,24 @@
1919
2024-12-21 -> ['394', '855,846']
2020
2024-12-20 -> ['833,950', '932']
2121
2024-12-18 -> ['951,792']
22-
2024-12-17 -> ['722']
22+
2024-12-17 -> ['722,380']
2323
2024-12-16 -> ['33,81']
2424
2024-12-14 -> ['253', '53,210,207', '163,1048']
2525
2024-12-13 -> ['776,31', '444', '298,729']
2626
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2727
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
28-
2024-12-09 -> ['722']
28+
2024-12-09 -> ['722,380']
2929
2024-12-08 -> ['33,81', '394', '737']
3030
2024-12-07 -> ['833,950', '900', '686,734,737']
3131
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
3232
2024-12-05 -> ['776,31', '528,334']
33-
2024-12-04 -> ['722', '004(todo),34(todo),162(todo),275(todo)']
33+
2024-12-04 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)']
3434
2024-12-03 -> ['33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
35-
2024-12-01 -> ['722', '253', '53,210,207']
35+
2024-12-01 -> ['722,380', '253', '53,210,207']
3636
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
37-
2024-11-29 -> ['722', '004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
38-
2024-11-28 -> ['722', '33,81', '253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
39-
2024-11-27 -> ['722', '33,81', '776,31', '951,792', '524,221,889']
37+
2024-11-29 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
38+
2024-11-28 -> ['722,380', '33,81', '253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
39+
2024-11-27 -> ['722,380', '33,81', '776,31', '951,792', '524,221,889']
4040
2024-11-26 -> ['33,81', '253', '004(todo),34(todo),162(todo),275(todo)', '743,889']
4141
2024-11-25 -> ['253', '776,31', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
4242
2024-11-24 -> ['253', '776,31', '004(todo),34(todo),162(todo),275(todo)', '833,950', '900']
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package LeetCodeJava.Design;
2+
3+
// https://leetcode.com/problems/insert-delete-getrandom-o1/description/
4+
5+
import java.util.*;
6+
7+
/**
8+
* 380. Insert Delete GetRandom O(1)
9+
* Solved
10+
* Medium
11+
* Topics
12+
* Companies
13+
* Implement the RandomizedSet class:
14+
*
15+
* RandomizedSet() Initializes the RandomizedSet object.
16+
* bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
17+
* bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
18+
* int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.
19+
* You must implement the functions of the class such that each function works in average O(1) time complexity.
20+
*
21+
*
22+
*
23+
* Example 1:
24+
*
25+
* Input
26+
* ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
27+
* [[], [1], [2], [2], [], [1], [2], []]
28+
* Output
29+
* [null, true, false, true, 2, true, false, 2]
30+
*
31+
* Explanation
32+
* RandomizedSet randomizedSet = new RandomizedSet();
33+
* randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
34+
* randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
35+
* randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
36+
* randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
37+
* randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
38+
* randomizedSet.insert(2); // 2 was already in the set, so return false.
39+
* randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
40+
*
41+
*
42+
* Constraints:
43+
*
44+
* -231 <= val <= 231 - 1
45+
* At most 2 * 105 calls will be made to insert, remove, and getRandom.
46+
* There will be at least one element in the data structure when getRandom is called.
47+
*
48+
*/
49+
public class InsertDeleteGetRandom0_1 {
50+
51+
/**
52+
* Your RandomizedSet object will be instantiated and called as such:
53+
* RandomizedSet obj = new RandomizedSet();
54+
* boolean param_1 = obj.insert(val);
55+
* boolean param_2 = obj.remove(val);
56+
* int param_3 = obj.getRandom();
57+
*/
58+
59+
// V0
60+
// TODO : implement
61+
class RandomizedSet {
62+
63+
/** NOTE !!! we use map as storage structure */
64+
Map<Integer, Integer> map;
65+
int count;
66+
Random random;
67+
68+
public RandomizedSet() {
69+
this.map = new HashMap<>();
70+
this.count = 0;
71+
this.random = new Random();
72+
}
73+
74+
public boolean insert(int val) {
75+
if (!this.map.containsKey(val)){
76+
this.map.put(val, 1);
77+
this.count += 1;
78+
return true;
79+
}
80+
return false;
81+
}
82+
83+
public boolean remove(int val) {
84+
if (this.map.containsKey(val)){
85+
this.map.remove(val);
86+
this.count -= 1;
87+
return true;
88+
}
89+
return false;
90+
}
91+
92+
public int getRandom() {
93+
int randomIdx = random.nextInt(this.count);
94+
// java get hashMap key as array
95+
// https://stackoverflow.com/questions/16203880/get-array-of-maps-keys
96+
Integer[] keyArray = this.map.keySet().toArray(new Integer[this.map.keySet().size()]);
97+
return keyArray[randomIdx];
98+
}
99+
}
100+
101+
// V1
102+
// IDEA : LIST + MAP (gpt)
103+
class RandomizedSet_1 {
104+
105+
/** Storage structure for elements and their indices */
106+
private Map<Integer, Integer> map; // Maps value to its index in the list
107+
private List<Integer> list; // Stores elements for constant-time access
108+
private Random random;
109+
110+
public RandomizedSet_1() {
111+
this.map = new HashMap<>();
112+
this.list = new ArrayList<>();
113+
this.random = new Random();
114+
}
115+
116+
public boolean insert(int val) {
117+
if (!this.map.containsKey(val)) {
118+
this.list.add(val); // Add value to the list
119+
this.map.put(val, this.list.size() - 1); // Map value to its index in the list
120+
return true;
121+
}
122+
return false; // Value already exists
123+
}
124+
125+
public boolean remove(int val) {
126+
if (this.map.containsKey(val)) {
127+
int idx = this.map.get(val); // Index of the value to remove
128+
int lastElement = this.list.get(this.list.size() - 1); // Last element in the list
129+
130+
// Swap the last element with the element to remove
131+
this.list.set(idx, lastElement);
132+
this.map.put(lastElement, idx);
133+
134+
// Remove the last element
135+
this.list.remove(this.list.size() - 1);
136+
this.map.remove(val);
137+
138+
return true;
139+
}
140+
return false; // Value does not exist
141+
}
142+
143+
public int getRandom() {
144+
int randomIdx = this.random.nextInt(this.list.size());
145+
return this.list.get(randomIdx); // Return a random element from the list
146+
}
147+
}
148+
149+
// V2
150+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4004,6 +4004,52 @@ public List<String> removeComments(String[] source) {
40044004
return tmp;
40054005
}
40064006

4007+
// LC 380
4008+
// https://leetcode.com/problems/insert-delete-getrandom-o1/
4009+
// 8.08 - 8.25 PM
4010+
class RandomizedSet {
4011+
4012+
//List<Integer> list;
4013+
Map<Integer, Integer> map;
4014+
int count;
4015+
Random random;
4016+
4017+
public RandomizedSet() {
4018+
//this.list = new ArrayList<>();
4019+
this.map = new HashMap<>();
4020+
this.count = 0;
4021+
this.random = new Random();
4022+
}
4023+
4024+
public boolean insert(int val) {
4025+
if (!this.map.containsKey(val)){
4026+
this.map.put(val, 1);
4027+
this.count += 1;
4028+
return true;
4029+
}
4030+
return false;
4031+
}
4032+
4033+
public boolean remove(int val) {
4034+
if (this.map.containsKey(val)){
4035+
this.map.remove(val);
4036+
this.count -= 1;
4037+
return true;
4038+
}
4039+
return false;
4040+
}
4041+
4042+
public int getRandom() {
4043+
//this.random = new Random();
4044+
int randomIdx = random.nextInt(this.count);
4045+
// TODO : check below
4046+
//int x = this.map.values().toArray().;
4047+
//new Arrays.
4048+
Integer[] keyArray = this.map.keySet().toArray(new Integer[this.map.keySet().size()]);
4049+
return keyArray[randomIdx];
4050+
}
4051+
}
4052+
40074053
}
40084054

40094055

0 commit comments

Comments
 (0)