|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -import java.util.HashMap; |
4 |
| -import java.util.Map; |
5 |
| -import java.util.Random; |
| 3 | +import java.util.*; |
6 | 4 |
|
7 | 5 | /**
|
8 | 6 | * 380. Insert Delete GetRandom O(1)
|
9 | 7 | * Design a data structure that supports all following operations in average O(1) time.
|
10 |
| - * <p> |
| 8 | + * |
11 | 9 | * insert(val): Inserts an item val to the set if not already present.
|
12 | 10 | * remove(val): Removes an item val from the set if present.
|
13 | 11 | * getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.
|
14 | 12 | * Example:
|
15 |
| - * <p> |
| 13 | + * |
16 | 14 | * // Init an empty set.
|
17 | 15 | * RandomizedSet randomSet = new RandomizedSet();
|
18 |
| - * <p> |
| 16 | + * |
19 | 17 | * // Inserts 1 to the set. Returns true as 1 was inserted successfully.
|
20 | 18 | * randomSet.insert(1);
|
21 |
| - * <p> |
| 19 | + * |
22 | 20 | * // Returns false as 2 does not exist in the set.
|
23 | 21 | * randomSet.remove(2);
|
24 |
| - * <p> |
| 22 | + * |
25 | 23 | * // Inserts 2 to the set, returns true. Set now contains [1,2].
|
26 | 24 | * randomSet.insert(2);
|
27 |
| - * <p> |
| 25 | + * |
28 | 26 | * // getRandom should return either 1 or 2 randomly.
|
29 | 27 | * randomSet.getRandom();
|
30 |
| - * <p> |
| 28 | + * |
31 | 29 | * // Removes 1 from the set, returns true. Set now contains [2].
|
32 | 30 | * randomSet.remove(1);
|
33 |
| - * <p> |
| 31 | + * |
34 | 32 | * // 2 was already in the set, so return false.
|
35 | 33 | * randomSet.insert(2);
|
36 |
| - * <p> |
| 34 | + * |
37 | 35 | * // Since 2 is the only number in the set, getRandom always return 2.
|
38 | 36 | * randomSet.getRandom();
|
39 | 37 | */
|
40 | 38 |
|
41 | 39 | public class _380 {
|
| 40 | + |
| 41 | + public static class Solution1 { |
| 42 | + /** |
| 43 | + * credit: https://leetcode.com/problems/insert-delete-getrandom-o1/discuss/85401/Java-solution-using-a-HashMap-and-an-ArrayList-along-with-a-follow-up.-(131-ms) |
| 44 | + */ |
| 45 | + public static class RandomizedSet { |
| 46 | + Map<Integer, Integer> locationMap; |
| 47 | + List<Integer> list; |
| 48 | + Random random; |
| 49 | + |
| 50 | + /** |
| 51 | + * Initialize your data structure here. |
| 52 | + */ |
| 53 | + public RandomizedSet() { |
| 54 | + locationMap = new HashMap<>(); |
| 55 | + list = new ArrayList<>(); |
| 56 | + random = new Random(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Inserts a value to the set. Returns true if the set did not already contain the specified element. |
| 61 | + */ |
| 62 | + public boolean insert(int val) { |
| 63 | + if (locationMap.containsKey(val)) { |
| 64 | + return false; |
| 65 | + } else { |
| 66 | + locationMap.put(val, list.size()); |
| 67 | + list.add(val); |
| 68 | + return true; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Removes a value from the set. Returns true if the set contained the specified element. |
| 74 | + */ |
| 75 | + public boolean remove(int val) { |
| 76 | + if (!locationMap.containsKey(val)) { |
| 77 | + return false; |
| 78 | + } else { |
| 79 | + int location = locationMap.get(val); |
| 80 | + /**if it's not the last one, then swap the last one with this val*/ |
| 81 | + if (location < list.size() - 1) { |
| 82 | + int lastOne = list.get(list.size() - 1); |
| 83 | + list.set(location, lastOne); |
| 84 | + locationMap.put(lastOne, location); |
| 85 | + } |
| 86 | + locationMap.remove(val); |
| 87 | + list.remove(list.size() - 1); |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get a random element from the set. |
| 94 | + */ |
| 95 | + public int getRandom() { |
| 96 | + return list.get(random.nextInt(list.size())); |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + } |
| 101 | + |
42 | 102 | /**
|
43 | 103 | * Your _380 object will be instantiated and called as such:
|
44 | 104 | * _380 obj = new _380();
|
45 | 105 | * boolean param_1 = obj.insert(val);
|
46 | 106 | * boolean param_2 = obj.delete(val);
|
47 | 107 | * int param_3 = obj.getRandom();
|
48 | 108 | */
|
49 |
| - public static class Solution1 { |
50 |
| - //TODO: this is not ideal, optimize it. |
| 109 | + public static class Solution2 { |
| 110 | + /**This is not ideal and not meeting average O(1) time for all three operations.*/ |
51 | 111 | public static class RandomizedSet {
|
52 | 112 |
|
53 | 113 | Map<Integer, Integer> forwardMap;
|
|
0 commit comments