-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertDeleteGetRandomWithDuplicateValues.java
More file actions
86 lines (73 loc) · 2.78 KB
/
InsertDeleteGetRandomWithDuplicateValues.java
File metadata and controls
86 lines (73 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.leetcode.problems.medium;
import java.util.*;
/**
* @author neeraj on 17/07/20
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class InsertDeleteGetRandomWithDuplicateValues {
public static void main(String[] args) {
RandomizedCollection obj = new RandomizedCollection();
obj.insert(1);
obj.insert(1);
obj.insert(2);
obj.insert(2);
obj.insert(2);
obj.insert(2);
obj.insert(2);
obj.insert(2);
for(int i=0;i<1000;i++) {
System.out.println(obj.getRandom());
}
}
static class RandomizedCollection {
/**
* Initialize your data structure here.
*/
List<Integer> data;
Map<Integer, Set<Integer>> dataToIndexMap;
Random random;
public RandomizedCollection() {
random = new Random();
data = new ArrayList<>();
dataToIndexMap = new HashMap<>();
}
/**
* Inserts a value to the collection. Returns true if the collection did not already contain the specified element.
*/
public boolean insert(int val) {
boolean exist = dataToIndexMap.containsKey(val); // We are allowing duplicates.
data.add(val); // append to last in the list
// Point to the node in list from hashMap
if (!exist) {
dataToIndexMap.put(val, new HashSet<>());
}
dataToIndexMap.get(val).add(data.size() - 1); // If duplicate adding multiple indexes.
return !exist;
}
/**
* Removes a value from the set. Returns true if the set contained the specified element.
*/
public boolean remove(int val) {
if (!dataToIndexMap.containsKey(val)) return false;
int index = dataToIndexMap.get(val).iterator().next();
dataToIndexMap.get(val).remove(index);
if (index < data.size() - 1) {
int lastItem = data.get(data.size() - 1);
data.set(index, lastItem); // Removing and putting the last item on this index.
dataToIndexMap.get(lastItem).remove(data.size() - 1); // Removing the last occurrence from the map for last Item.
dataToIndexMap.get(lastItem).add(index); // Setting the removed item index in the map as the lastItem index.
}
data.remove(data.size() - 1);
if (dataToIndexMap.get(val).isEmpty()) dataToIndexMap.remove(val);
return true;
}
/**
* Get a random element from the set.
*/
public int getRandom() {
int randomValue = random.nextInt(data.size());
return data.get(randomValue); // O(1) index based access.
}
}
}