Skip to content

Commit 34bcaa3

Browse files
refactor 380
1 parent 376c430 commit 34bcaa3

File tree

1 file changed

+74
-14
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+74
-14
lines changed

src/main/java/com/fishercoder/solutions/_380.java

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,113 @@
11
package com.fishercoder.solutions;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
import java.util.Random;
3+
import java.util.*;
64

75
/**
86
* 380. Insert Delete GetRandom O(1)
97
* Design a data structure that supports all following operations in average O(1) time.
10-
* <p>
8+
*
119
* insert(val): Inserts an item val to the set if not already present.
1210
* remove(val): Removes an item val from the set if present.
1311
* getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.
1412
* Example:
15-
* <p>
13+
*
1614
* // Init an empty set.
1715
* RandomizedSet randomSet = new RandomizedSet();
18-
* <p>
16+
*
1917
* // Inserts 1 to the set. Returns true as 1 was inserted successfully.
2018
* randomSet.insert(1);
21-
* <p>
19+
*
2220
* // Returns false as 2 does not exist in the set.
2321
* randomSet.remove(2);
24-
* <p>
22+
*
2523
* // Inserts 2 to the set, returns true. Set now contains [1,2].
2624
* randomSet.insert(2);
27-
* <p>
25+
*
2826
* // getRandom should return either 1 or 2 randomly.
2927
* randomSet.getRandom();
30-
* <p>
28+
*
3129
* // Removes 1 from the set, returns true. Set now contains [2].
3230
* randomSet.remove(1);
33-
* <p>
31+
*
3432
* // 2 was already in the set, so return false.
3533
* randomSet.insert(2);
36-
* <p>
34+
*
3735
* // Since 2 is the only number in the set, getRandom always return 2.
3836
* randomSet.getRandom();
3937
*/
4038

4139
public class _380 {
40+
41+
public static class Solution1 {
42+
/**
43+
* credit: https://leetcode.com/problems/insert-delete-getrandom-o1/discuss/85401/Java-solution-using-a-HashMap-and-an-ArrayList-along-with-a-follow-up.-(131-ms)
44+
*/
45+
public static class RandomizedSet {
46+
Map<Integer, Integer> locationMap;
47+
List<Integer> list;
48+
Random random;
49+
50+
/**
51+
* Initialize your data structure here.
52+
*/
53+
public RandomizedSet() {
54+
locationMap = new HashMap<>();
55+
list = new ArrayList<>();
56+
random = new Random();
57+
}
58+
59+
/**
60+
* Inserts a value to the set. Returns true if the set did not already contain the specified element.
61+
*/
62+
public boolean insert(int val) {
63+
if (locationMap.containsKey(val)) {
64+
return false;
65+
} else {
66+
locationMap.put(val, list.size());
67+
list.add(val);
68+
return true;
69+
}
70+
}
71+
72+
/**
73+
* Removes a value from the set. Returns true if the set contained the specified element.
74+
*/
75+
public boolean remove(int val) {
76+
if (!locationMap.containsKey(val)) {
77+
return false;
78+
} else {
79+
int location = locationMap.get(val);
80+
/**if it's not the last one, then swap the last one with this val*/
81+
if (location < list.size() - 1) {
82+
int lastOne = list.get(list.size() - 1);
83+
list.set(location, lastOne);
84+
locationMap.put(lastOne, location);
85+
}
86+
locationMap.remove(val);
87+
list.remove(list.size() - 1);
88+
return true;
89+
}
90+
}
91+
92+
/**
93+
* Get a random element from the set.
94+
*/
95+
public int getRandom() {
96+
return list.get(random.nextInt(list.size()));
97+
}
98+
99+
}
100+
}
101+
42102
/**
43103
* Your _380 object will be instantiated and called as such:
44104
* _380 obj = new _380();
45105
* boolean param_1 = obj.insert(val);
46106
* boolean param_2 = obj.delete(val);
47107
* int param_3 = obj.getRandom();
48108
*/
49-
public static class Solution1 {
50-
//TODO: this is not ideal, optimize it.
109+
public static class Solution2 {
110+
/**This is not ideal and not meeting average O(1) time for all three operations.*/
51111
public static class RandomizedSet {
52112

53113
Map<Integer, Integer> forwardMap;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy