/**
Design a data structure that supports all following operations in average O(1) time.
Note: Duplicate elements are allowed.
insert(val): Inserts an item val to the collection.
remove(val): Removes an item val from the collection if present.
getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.
Example:
// Init an empty collection.
RandomizedCollection collection = new RandomizedCollection();
// Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1);
// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].
collection.insert(1);
// Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
collection.insert(2);
// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
collection.getRandom();
// Removes 1 from the collection, returns true. Collection now contains [1,2].
collection.remove(1);
// getRandom should return 1 and 2 both equally likely.
collection.getRandom();
*/
//class to hold the item and its indexes
class Item{
//we can only store the list of indexes also
int val;
List<Integer> indexes;
public Item(int v, int size){
val = v;
indexes = new ArrayList<>();
indexes.add(size);
}
//setters getters
}
//I only show the add and remove method, the rest of logic is similar to InsertDeleteGetRandomO1 without duplicates
class RandomizedSet {
HashMap<Integer, Item> map;
ArrayList<Integer> values;
/** Initialize your data structure here. */
public RandomizedSet() {
map = new HashMap<Integer, Item>();
values = new ArrayList<Integer>();
}
public void add(int v){
if(!map.containsKey(v)){
map.put(v, new Item(v, vals.size()));
}else{
Item it = map.get(v);
it.add(values.size());
map.put(v, it);
}
values.add(v);
}
public boolean remove(int v){
if(!map.containsKey(va)){
return false;
}
Item it = map.get(v);
int loc = it.indexes.get(0);
it.indexes.remove(0);
if(it.indexes.size()<1){
map.remove(v);
}else{
map.put(v, it);
}
if(loc <values.size()-1){
int lastval = values.get(values.size()-1);
values.set(loc, lastval);
}
values.remove(values.size()-1);
return true;
}
/** Get a random element from the set. */
public int getRandom() {
return values.get(rand.nextInt(values.size()));
}
}
Design a data structure that supports all following operations in average O(1) time.
Note: Duplicate elements are allowed.
insert(val): Inserts an item val to the collection.
remove(val): Removes an item val from the collection if present.
getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.
Example:
// Init an empty collection.
RandomizedCollection collection = new RandomizedCollection();
// Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1);
// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].
collection.insert(1);
// Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
collection.insert(2);
// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
collection.getRandom();
// Removes 1 from the collection, returns true. Collection now contains [1,2].
collection.remove(1);
// getRandom should return 1 and 2 both equally likely.
collection.getRandom();
*/
//class to hold the item and its indexes
class Item{
//we can only store the list of indexes also
int val;
List<Integer> indexes;
public Item(int v, int size){
val = v;
indexes = new ArrayList<>();
indexes.add(size);
}
//setters getters
}
//I only show the add and remove method, the rest of logic is similar to InsertDeleteGetRandomO1 without duplicates
class RandomizedSet {
HashMap<Integer, Item> map;
ArrayList<Integer> values;
/** Initialize your data structure here. */
public RandomizedSet() {
map = new HashMap<Integer, Item>();
values = new ArrayList<Integer>();
}
public void add(int v){
if(!map.containsKey(v)){
map.put(v, new Item(v, vals.size()));
}else{
Item it = map.get(v);
it.add(values.size());
map.put(v, it);
}
values.add(v);
}
public boolean remove(int v){
if(!map.containsKey(va)){
return false;
}
Item it = map.get(v);
int loc = it.indexes.get(0);
it.indexes.remove(0);
if(it.indexes.size()<1){
map.remove(v);
}else{
map.put(v, it);
}
if(loc <values.size()-1){
int lastval = values.get(values.size()-1);
values.set(loc, lastval);
}
values.remove(values.size()-1);
return true;
}
/** Get a random element from the set. */
public int getRandom() {
return values.get(rand.nextInt(values.size()));
}
}
No comments:
Post a Comment