Skip to content

Commit

Permalink
add 380 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 27, 2024
1 parent fa1ac01 commit f39be36
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@
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 |
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 |🔒| |
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)
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 || |
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 || |
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)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20241126: 722
20241126: 722,380
20241125: 33,81
20241124: 153
20241123: 253
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2025-01-20 -> ['722']
2025-01-20 -> ['722,380']
2025-01-19 -> ['33,81']
2025-01-17 -> ['253']
2025-01-16 -> ['776,31']
Expand All @@ -9,7 +9,7 @@
2025-01-04 -> ['53,210,207']
2025-01-03 -> ['444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-30 -> ['722']
2024-12-30 -> ['722,380']
2024-12-29 -> ['33,81']
2024-12-28 -> ['900']
2024-12-27 -> ['253', '26,27', '802,1197,26']
Expand All @@ -19,24 +19,24 @@
2024-12-21 -> ['394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-18 -> ['951,792']
2024-12-17 -> ['722']
2024-12-17 -> ['722,380']
2024-12-16 -> ['33,81']
2024-12-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['776,31', '444', '298,729']
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-09 -> ['722']
2024-12-09 -> ['722,380']
2024-12-08 -> ['33,81', '394', '737']
2024-12-07 -> ['833,950', '900', '686,734,737']
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
2024-12-05 -> ['776,31', '528,334']
2024-12-04 -> ['722', '004(todo),34(todo),162(todo),275(todo)']
2024-12-04 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)']
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']
2024-12-01 -> ['722', '253', '53,210,207']
2024-12-01 -> ['722,380', '253', '53,210,207']
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
2024-11-29 -> ['722', '004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
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)']
2024-11-27 -> ['722', '33,81', '776,31', '951,792', '524,221,889']
2024-11-29 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
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)']
2024-11-27 -> ['722,380', '33,81', '776,31', '951,792', '524,221,889']
2024-11-26 -> ['33,81', '253', '004(todo),34(todo),162(todo),275(todo)', '743,889']
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']
2024-11-24 -> ['253', '776,31', '004(todo),34(todo),162(todo),275(todo)', '833,950', '900']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package LeetCodeJava.Design;

// https://leetcode.com/problems/insert-delete-getrandom-o1/description/

import java.util.*;

/**
* 380. Insert Delete GetRandom O(1)
* Solved
* Medium
* Topics
* Companies
* Implement the RandomizedSet class:
*
* RandomizedSet() Initializes the RandomizedSet object.
* bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
* bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
* 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.
* You must implement the functions of the class such that each function works in average O(1) time complexity.
*
*
*
* Example 1:
*
* Input
* ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
* [[], [1], [2], [2], [], [1], [2], []]
* Output
* [null, true, false, true, 2, true, false, 2]
*
* Explanation
* RandomizedSet randomizedSet = new RandomizedSet();
* randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
* randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
* randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
* randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
* randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
* randomizedSet.insert(2); // 2 was already in the set, so return false.
* randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
*
*
* Constraints:
*
* -231 <= val <= 231 - 1
* At most 2 * 105 calls will be made to insert, remove, and getRandom.
* There will be at least one element in the data structure when getRandom is called.
*
*/
public class InsertDeleteGetRandom0_1 {

/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet obj = new RandomizedSet();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/

// V0
// TODO : implement
class RandomizedSet {

/** NOTE !!! we use map as storage structure */
Map<Integer, Integer> map;
int count;
Random random;

public RandomizedSet() {
this.map = new HashMap<>();
this.count = 0;
this.random = new Random();
}

public boolean insert(int val) {
if (!this.map.containsKey(val)){
this.map.put(val, 1);
this.count += 1;
return true;
}
return false;
}

public boolean remove(int val) {
if (this.map.containsKey(val)){
this.map.remove(val);
this.count -= 1;
return true;
}
return false;
}

public int getRandom() {
int randomIdx = random.nextInt(this.count);
// java get hashMap key as array
// https://stackoverflow.com/questions/16203880/get-array-of-maps-keys
Integer[] keyArray = this.map.keySet().toArray(new Integer[this.map.keySet().size()]);
return keyArray[randomIdx];
}
}

// V1
// IDEA : LIST + MAP (gpt)
class RandomizedSet_1 {

/** Storage structure for elements and their indices */
private Map<Integer, Integer> map; // Maps value to its index in the list
private List<Integer> list; // Stores elements for constant-time access
private Random random;

public RandomizedSet_1() {
this.map = new HashMap<>();
this.list = new ArrayList<>();
this.random = new Random();
}

public boolean insert(int val) {
if (!this.map.containsKey(val)) {
this.list.add(val); // Add value to the list
this.map.put(val, this.list.size() - 1); // Map value to its index in the list
return true;
}
return false; // Value already exists
}

public boolean remove(int val) {
if (this.map.containsKey(val)) {
int idx = this.map.get(val); // Index of the value to remove
int lastElement = this.list.get(this.list.size() - 1); // Last element in the list

// Swap the last element with the element to remove
this.list.set(idx, lastElement);
this.map.put(lastElement, idx);

// Remove the last element
this.list.remove(this.list.size() - 1);
this.map.remove(val);

return true;
}
return false; // Value does not exist
}

public int getRandom() {
int randomIdx = this.random.nextInt(this.list.size());
return this.list.get(randomIdx); // Return a random element from the list
}
}

// V2
}
46 changes: 46 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -4004,6 +4004,52 @@ public List<String> removeComments(String[] source) {
return tmp;
}

// LC 380
// https://leetcode.com/problems/insert-delete-getrandom-o1/
// 8.08 - 8.25 PM
class RandomizedSet {

//List<Integer> list;
Map<Integer, Integer> map;
int count;
Random random;

public RandomizedSet() {
//this.list = new ArrayList<>();
this.map = new HashMap<>();
this.count = 0;
this.random = new Random();
}

public boolean insert(int val) {
if (!this.map.containsKey(val)){
this.map.put(val, 1);
this.count += 1;
return true;
}
return false;
}

public boolean remove(int val) {
if (this.map.containsKey(val)){
this.map.remove(val);
this.count -= 1;
return true;
}
return false;
}

public int getRandom() {
//this.random = new Random();
int randomIdx = random.nextInt(this.count);
// TODO : check below
//int x = this.map.values().toArray().;
//new Arrays.
Integer[] keyArray = this.map.keySet().toArray(new Integer[this.map.keySet().size()]);
return keyArray[randomIdx];
}
}

}


0 comments on commit f39be36

Please sign in to comment.