您的位置:首页 > 其它

LeetCode Insert Delete GetRandom O(1) - Duplicates allowed

2017-02-24 19:09 941 查看
原题链接在这里: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.

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();

题解:

类似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  */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: