Insert Delete GetRandom O(1) I & II

时间:2023-12-10 18:33:38

Design a data structure that supports all following operations in O(1) time.

insert(val): Inserts an item val to the set if not already present.
remove(val): Removes an item val from the set if present.
getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.

分析:

因为要求所有操作O(1),插入可以满足这个条件,如果用数组来存储值,getRandom可以满足O(1).但是要求不能重复和delete的时候也满足这个条件,只能使用一个map. key是值,value是位置。

 public class RandomizedSet {
ArrayList<Integer> nums;//值
// value to position
HashMap<Integer, Integer> valueToPositionMap;
Random rand; public RandomizedSet() {
nums = new ArrayList<>();
valueToPositionMap = new HashMap<>();
rand = new Random(System.currentTimeMillis());
} /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
public boolean insert(int val) {
boolean isContained = valueToPositionMap.containsKey(val);
if ( isContained ) return false;
valueToPositionMap.put( val, nums.size());
nums.add(val);
return true;
} /** Removes a value from the set. Returns true if the set contained the specified element. */
public boolean remove(int val) {
boolean isContained = valueToPositionMap.containsKey(val);
if ( ! isContained ) return false;
int position = valueToPositionMap.get(val);
if (position != nums.size() - ) {
// put the true last one to 'position' in nums
int lastOne = nums.get(nums.size() - );
nums.set( position , lastOne );
valueToPositionMap.put(lastOne, position);
}
valueToPositionMap.remove(val);
nums.remove(nums.size() - );
return true;
} /** Get a random element from the set. */
public int getRandom() {
return nums.get( rand.nextInt(nums.size()) );
}
}

第二种情况:允许有重复数值。

用set来记录同一个值出现的位置,如果被删除的数不是最后一个,则和最后一个换一下位置,然后删除就可以了。

 public class RandomizedCollection {
List<Integer> nums;
Map<Integer, Set<Integer>> valueToPositionMap;
Random rand; public RandomizedCollection() {
nums = new ArrayList<>();
valueToPositionMap = new HashMap<>();
rand = new Random(System.currentTimeMillis());
} /**
* Inserts a value to the collection. Returns true if the collection did not
* already contain the specified element.
*/
public boolean insert(int val) {
boolean isContained = valueToPositionMap.containsKey(val);
if (!isContained) {
valueToPositionMap.put(val, new HashSet<>());
}
valueToPositionMap.get(val).add(nums.size());
nums.add(val);
return !isContained;
} /**
* Removes a value from the collection. Returns true if the collection contained
* the specified element.
*/
public boolean remove(int val) {
if (!valueToPositionMap.containsKey(val)) {
return false;
}
if (!valueToPositionMap.get(val).contains(nums.size() - )) {
int currPos = valueToPositionMap.get(val).iterator().next();
int lastVal = nums.get(nums.size() - );
valueToPositionMap.get(lastVal).remove(nums.size() - );
valueToPositionMap.get(lastVal).add(currPos);
valueToPositionMap.get(val).remove(currPos);
valueToPositionMap.get(val).add(nums.size() - );
nums.set(currPos, lastVal);
}
valueToPositionMap.get(val).remove(nums.size() - );
if (valueToPositionMap.get(val).isEmpty()) {
valueToPositionMap.remove(val);
}
nums.remove(nums.size() - );
return true;
} /** Get a random element from the collection. */
public int getRandom() {
return nums.get(rand.nextInt(nums.size()));
}
}