LeetCode Insert Delete GetRandom O(1) - Duplicates allowed

LeetCode 题解:Insert Delete GetRandom O(1)
本文提供了一种数据结构的设计方案,该结构支持 insert、remove 和 getRandom 操作,并确保这些操作的平均时间复杂度为 O(1)。通过使用 List 和 HashMap 的组合实现,确保了即使允许重复元素也能高效地完成任务。

原题链接在这里:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/?tab=Description

题目:

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

Note: Duplicate elements are allowed.

 

  1. insert(val): Inserts an item val to the collection.
  2. remove(val): Removes an item val from the collection if present.
  3. 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();

题解:

类似Insert Delete GetRandom O(1).

用set来保存val的所有index.

Note: remove时,要在if前从hm.get(val)中remove掉ind.

e.g. insert 4, insert 4, remove 4, 4对应的index 是<4, (0,1)> 

if中remove掉了最后的index 1, 又添加了一遍找到的index 0, 若是放在if后面remove ind的话, 会把0也从4对应的index中remove掉.

Time Complexity: insert, O(1). remove, O(1). getRandom, O(1).

Space: O(n).

AC Java:

 1 public class RandomizedCollection {
 2     List<Integer> ls;
 3     HashMap<Integer, Set<Integer>> hm;
 4     Random rand;
 5     
 6     /** Initialize your data structure here. */
 7     public RandomizedCollection() {
 8         ls = new ArrayList<Integer>();
 9         hm = new HashMap<Integer, Set<Integer>>();
10         rand = new Random();
11     }
12     
13     /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
14     public boolean insert(int val) {
15         boolean flag = !hm.containsKey(val);
16         if(flag){
17             hm.put(val, new LinkedHashSet<Integer>());    
18         }
19         
20         ls.add(val);
21         hm.get(val).add(ls.size()-1);
22         return flag;
23     }
24     
25     /** Removes a value from the collection. Returns true if the collection contained the specified element. */
26     public boolean remove(int val) {
27         if(!hm.containsKey(val)){
28             return false;
29         }
30         
31         int ind = hm.get(val).iterator().next();
32         //这里要在if前从hm.get(val)中remove掉ind
33         //e.g. insert 4, insert 4, remove 4, 4对应的index 是<4, (0,1)> 
34         //if中remove掉了最后的index 1, 又添加了一遍找到的index 0
35         //若是放在if后面remove ind的话, 会把0也从4对应的index中remove掉.
36         hm.get(val).remove(ind);
37         
38         if(ind < ls.size()-1){
39             int lastElement = ls.get(ls.size()-1);
40             ls.set(ind, lastElement);
41             hm.get(lastElement).remove(ls.size()-1);
42             hm.get(lastElement).add(ind);
43         }
44         
45         ls.remove(ls.size()-1);
46         if(hm.get(val).size() == 0){
47             hm.remove(val);
48         }
49         return true;
50     }
51     
52     /** Get a random element from the collection. */
53     public int getRandom() {
54         return ls.get(rand.nextInt(ls.size()));
55     }
56 }
57 
58 /**
59  * Your RandomizedCollection object will be instantiated and called as such:
60  * RandomizedCollection obj = new RandomizedCollection();
61  * boolean param_1 = obj.insert(val);
62  * boolean param_2 = obj.remove(val);
63  * int param_3 = obj.getRandom();
64  */

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6439914.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值