Skip to content

Commit f396537

Browse files
committed
Add solution of RandomizedSet task
1 parent 0b00bde commit f396537

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package by.andd3dfx.collections;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Random;
8+
9+
/**
10+
* <pre>
11+
* <a href="https://leetcode.com/problems/insert-delete-getrandom-o1/description/">Task description</a>
12+
*
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+
*
20+
* You must implement the functions of the class such that each function works in average O(1) time complexity.
21+
*
22+
* Example 1:
23+
* Input
24+
* ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
25+
* [[], [1], [2], [2], [], [1], [2], []]
26+
* Output
27+
* [null, true, false, true, 2, true, false, 2]
28+
*
29+
* Explanation
30+
* RandomizedSet randomizedSet = new RandomizedSet();
31+
* randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
32+
* randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
33+
* randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
34+
* randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
35+
* randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
36+
* randomizedSet.insert(2); // 2 was already in the set, so return false.
37+
* randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
38+
* </pre>
39+
*/
40+
public class RandomizedSet {
41+
42+
private final Random random = new Random();
43+
private final Map<Integer, Integer> map = new HashMap<>();
44+
private final List<Integer> keys = new ArrayList<>();
45+
46+
/**
47+
* Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
48+
*/
49+
public boolean insert(int val) {
50+
var isNotExist = !map.containsKey(val);
51+
if (isNotExist) {
52+
keys.add(val);
53+
map.put(val, keys.size() - 1);
54+
}
55+
return isNotExist;
56+
}
57+
58+
/**
59+
* Removes an item val from the set if present. Returns true if the item was present, false otherwise.
60+
*/
61+
public boolean remove(int val) {
62+
var isExist = map.containsKey(val);
63+
if (isExist) {
64+
var index = map.get(val);
65+
keys.set(index, keys.getLast());
66+
map.put(keys.get(index), index);
67+
keys.removeLast();
68+
map.remove(val);
69+
}
70+
return isExist;
71+
}
72+
73+
/**
74+
* Returns a random element from the current set of elements (it's guaranteed that at least one element exists
75+
* when this method is called). Each element must have the same probability of being returned.
76+
*/
77+
public int getRandom() {
78+
var index = random.nextInt(keys.size());
79+
return keys.get(index);
80+
}
81+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package by.andd3dfx.collections;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class RandomizedSetTest {
11+
12+
private RandomizedSet randomizedSet;
13+
14+
@Before
15+
public void setUp() throws Exception {
16+
randomizedSet = new RandomizedSet();
17+
}
18+
19+
@Test
20+
public void insert() {
21+
RandomizedSet randomizedSet = new RandomizedSet();
22+
assertTrue(randomizedSet.insert(1)); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
23+
assertFalse(randomizedSet.remove(2)); // Returns false as 2 does not exist in the set.
24+
assertTrue(randomizedSet.insert(2)); // Inserts 2 to the set, returns true. Set now contains [1,2].
25+
assertThat(randomizedSet.getRandom()).isIn(1, 2); // getRandom() should return either 1 or 2 randomly.
26+
assertTrue(randomizedSet.remove(1)); // Removes 1 from the set, returns true. Set now contains [2].
27+
assertFalse(randomizedSet.insert(2)); // 2 was already in the set, so return false.
28+
assertThat(randomizedSet.getRandom()).isEqualTo(2); // Since 2 is the only number in the set, getRandom() will always return 2.
29+
}
30+
}

0 commit comments

Comments
 (0)