Leetcode-381-O(1) 时间插入、删除和获取随机元素 - 允许重复

Leetcode-381-O(1) 时间插入、删除和获取随机元素 - 允许重复

思路:哈希表

题目描述

设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构。

注意: 允许出现重复元素。

1
2
3
insert(val):向集合中插入元素 val。
remove(val):当 val 存在时,从集合中移除一个 val。
getRandom:从现有集合中随机获取一个元素。每个元素被返回的概率应该与其在集合中的数量呈线性相关。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
示例:

// 初始化一个空的集合。
RandomizedCollection collection = new RandomizedCollection();

// 向集合中插入 1 。返回 true 表示集合不包含 1 。
collection.insert(1);

// 向集合中插入另一个 1 。返回 false 表示集合包含 1 。集合现在包含 [1,1] 。
collection.insert(1);

// 向集合中插入 2 ,返回 true 。集合现在包含 [1,1,2] 。
collection.insert(2);

// getRandom 应当有 2/3 的概率返回 1 ,1/3 的概率返回 2 。
collection.getRandom();

// 从集合中删除 1 ,返回 true 。集合现在包含 [1,2] 。
collection.remove(1);

// getRandom 应有相同概率返回 1 和 2 。
collection.getRandom();

方法 : 哈希表

  • 对于get() 方法
    • 为了使得 O(1) 时间内能够随机获取一个元素,我们将每个数值(可以重复)存储在一个列表nums 中
    • 这样,获取随机元素时,只需要随机生成一个列表中的索引,就能够得到一个随机元素。
  • 对于 remove() 方法
    • 这样做的问题在于:列表中的随机删除并不是 O(1) 的。
    • 然而我们可以发现,列表中元素的顺序是无关紧要的,只要它们正确地存在于列表中即可。
    • 因此,在删除元素时,我们可以将被删的元素与列表中最后一个元素交换位置,随后便可以在 O(1)时间内,从列表中去除该元素。
    • 这需要我们额外维护数值在列表中每一次出现的下标集合。对于数值 val 而言,记其下标集合为 Sidx。

具体删除操作

  • 在删除时,我们找出 val出现的其中一个下标 i
  • 并将 nums[i]nums[nums.length - 1] 交换。
  • 随后,将 i 从 Sval 中删除,并将Snums[nums.length - 1] 中原有的 nums[nums.length - 1] 替换成 i 。
  • 由于每个操作都是O(1)的 那么平均时间复杂度也是 O(1)
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
class RandomizedCollection {
Map<Integer,Set<Integer>> idx; // Map 用于维护数值在列表中每次出现的下标集合 Set 用于作为下标集合
List<Integer> nums; // 用于随机获取元素 get() 方法

/** Initialize your data structure here. */
public RandomizedCollection() {
idx = new HashMap<Integer,Set<Integer>>();
nums = new ArrayList<Integer>();
}

/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
// 插入操作
nums.add(val); // 加入到列表中

// 记录出现的下标集合
// key 是 出现的数字
// val 是 出现数字所对应的下标
// 比如插入[1,1,1,2,2,2]
// 对应idx 即为 [1,[0,1,2]] [2,[3,4,5]]
Set<Integer> set = idx.getOrDefault(val,new HashSet<Integer>());
set.add(nums.size() - 1);
idx.put(val,set); // set记录每个数字每一次出现的下标
return set.size() == 1;
}

/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
// 删除操作
if(!idx.containsKey(val)){ // 如果根本没有这个数字的话,直接返回false
return false;
}

// 在删除时 找到val出现的其中一个下标i
// 并将其和 最后一个元素进行交换
Iterator<Integer> it = idx.get(val).iterator();
int i = it.next();
int lastNum = nums.get(nums.size() - 1);
nums.set(i,lastNum);

idx.get(val).remove(i);
idx.get(lastNum).remove(nums.size() - 1);

if(i < nums.size() - 1){
idx.get(lastNum).add(i);
}
if(idx.get(val).size() == 0){
idx.remove(val);
}

nums.remove(nums.size() - 1);

return true;
}

/** Get a random element from the collection. */
public int getRandom() {
return nums.get((int) (Math.random() * nums.size()));
}
}

/**
* Your RandomizedCollection object will be instantiated and called as such:
* RandomizedCollection obj = new RandomizedCollection();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/

复杂度分析

  • 时间复杂度 O(1)
  • 空间复杂度 O(n)
打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2019-2022 Zhuuu
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信