|
1 | 1 | package com.fishercoder.solutions.firstthousand;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
3 | 4 | import java.util.HashMap;
|
| 5 | +import java.util.LinkedHashSet; |
| 6 | +import java.util.List; |
4 | 7 | import java.util.Map;
|
5 | 8 | import java.util.Random;
|
| 9 | +import java.util.Set; |
6 | 10 |
|
7 | 11 | public class _381 {
|
8 | 12 | public static class Solution1 {
|
9 |
| - |
10 |
| - Map<Integer, Integer> forwardMap; |
11 |
| - //key is the to-be-inserted number, value is its auto-incremented index |
12 |
| - Map<Integer, Integer> reverseMap;//the other way around |
13 |
| - int index; |
14 |
| - Random rand; |
15 |
| - |
16 | 13 | /**
|
17 |
| - * Initialize your data structure here. |
| 14 | + * This is a natural extension to the solution from https://leetcode.com/problems/insert-delete-getrandom-o1 |
| 15 | + * You only need to change the value type of the hashmap to be a set instead of Integer to hold all indexes for this value ever inserted. |
18 | 16 | */
|
19 |
| - public Solution1() { |
20 |
| - forwardMap = new HashMap(); |
21 |
| - reverseMap = new HashMap(); |
22 |
| - index = 0; |
23 |
| - rand = new Random(); |
24 |
| - } |
25 | 17 |
|
26 |
| - /** |
27 |
| - * Inserts a value to the collection. Returns true if the collection did not already contain the |
28 |
| - * specified element. |
29 |
| - */ |
30 |
| - public boolean insert(int val) { |
31 |
| - boolean contains; |
32 |
| - if (reverseMap.containsValue(val)) { |
33 |
| - contains = true; |
34 |
| - } else { |
35 |
| - contains = false; |
| 18 | + public static class RandomizedCollection { |
| 19 | + List<Integer> list; |
| 20 | + Map<Integer, Set<Integer>> map; |
| 21 | + Random random; |
| 22 | + |
| 23 | + public RandomizedCollection() { |
| 24 | + this.list = new ArrayList<>(); |
| 25 | + this.map = new HashMap<>(); |
| 26 | + this.random = new Random(); |
36 | 27 | }
|
37 |
| - forwardMap.put(val, |
38 |
| - index);//this will overwrite the preivous key with a new index if the key already exists |
39 |
| - reverseMap.put(index, val); |
40 |
| - index++; |
41 |
| - return contains; |
42 |
| - } |
43 | 28 |
|
44 |
| - /** |
45 |
| - * Removes a value from the collection. Returns true if the collection contained the specified |
46 |
| - * element. |
47 |
| - */ |
48 |
| - public boolean remove(int val) { |
49 |
| - boolean contains; |
50 |
| - if (reverseMap.containsValue(val)) { |
51 |
| - contains = true; |
52 |
| - if (forwardMap.containsKey(val)) { |
53 |
| - int i = forwardMap.get(val); |
54 |
| - forwardMap.remove(val); |
55 |
| - reverseMap.remove(i); |
| 29 | + public boolean insert(int val) { |
| 30 | + Set<Integer> indexSet = map.getOrDefault(val, new LinkedHashSet<>()); |
| 31 | + indexSet.add(list.size()); |
| 32 | + map.put(val, indexSet); |
| 33 | + list.add(val); |
| 34 | + return indexSet.size() <= 1; |
| 35 | + } |
| 36 | + |
| 37 | + public boolean remove(int val) { |
| 38 | + if (!map.containsKey(val) || map.get(val).size() == 0) { |
| 39 | + return false; |
56 | 40 | } else {
|
57 |
| - //remove the entry in revserve map that has val as its value |
58 |
| - reverseMap.values().remove(val); |
| 41 | + int removeIndex = map.get(val).iterator().next(); |
| 42 | + map.get(val).remove(removeIndex); |
| 43 | + int lastElement = list.get(list.size() - 1); |
| 44 | + list.set(removeIndex, lastElement); |
| 45 | + map.get(lastElement).add(removeIndex); |
| 46 | + map.get(lastElement).remove(list.size() - 1); |
| 47 | + list.remove(list.size() - 1); |
| 48 | + return true; |
59 | 49 | }
|
60 |
| - } else { |
61 |
| - contains = false; |
62 | 50 | }
|
63 |
| - return contains; |
64 |
| - } |
65 | 51 |
|
66 |
| - /** |
67 |
| - * Get a random element from the collection. |
68 |
| - */ |
69 |
| - public int getRandom() { |
70 |
| - int randNum = rand.nextInt(index); |
71 |
| - while (!reverseMap.containsKey(randNum)) { |
72 |
| - randNum = rand.nextInt(index); |
| 52 | + public int getRandom() { |
| 53 | + return list.get(random.nextInt(list.size())); |
73 | 54 | }
|
74 |
| - return reverseMap.get(randNum); |
75 | 55 | }
|
76 | 56 | }
|
77 | 57 | }
|
0 commit comments