Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3394b6d

Browse files
committed
Add solution #381
1 parent e186a32 commit 3394b6d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* 381. Insert Delete GetRandom O(1) - Duplicates allowed
3+
* https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/
4+
* Difficulty: Hard
5+
*
6+
* RandomizedCollection is a data structure that contains a collection of numbers, possibly
7+
* duplicates (i.e., a multiset). It should support inserting and removing specific elements
8+
* and also reporting a random element.
9+
*
10+
* Implement the RandomizedCollection class:
11+
* - RandomizedCollection() Initializes the empty RandomizedCollection object.
12+
* - bool insert(int val) Inserts an item val into the multiset, even if the item is already
13+
* present. Returns true if the item is not present, false otherwise.
14+
* - bool remove(int val) Removes an item val from the multiset if present. Returns true if
15+
* the item is present, false otherwise. Note that if val has multiple occurrences in the
16+
* multiset, we only remove one of them.
17+
* - int getRandom() Returns a random element from the current multiset of elements. The
18+
* probability of each element being returned is linearly related to the number of the same
19+
* values the multiset contains.
20+
*
21+
* You must implement the functions of the class such that each function works on average O(1)
22+
* time complexity.
23+
*
24+
* Note: The test cases are generated such that getRandom will only be called if there is at
25+
* least one item in the RandomizedCollection.
26+
*/
27+
28+
var RandomizedCollection = function() {
29+
this.values = [];
30+
this.map = new Map();
31+
};
32+
33+
/**
34+
* @param {number} val
35+
* @return {boolean}
36+
*/
37+
RandomizedCollection.prototype.insert = function(val) {
38+
this.values.push(val);
39+
const values = this.map.get(val) || new Set();
40+
values.add(this.values.length - 1);
41+
this.map.set(val, values);
42+
return values.size === 1;
43+
};
44+
45+
/**
46+
* @param {number} val
47+
* @return {boolean}
48+
*/
49+
RandomizedCollection.prototype.remove = function(val) {
50+
if (!this.map.has(val)) return false;
51+
const values = this.map.get(val);
52+
const index = values.values().next().value;
53+
values.delete(index);
54+
if (index < this.values.length - 1) {
55+
const last = this.values.pop();
56+
this.values[index] = last;
57+
const lastValues = this.map.get(last);
58+
lastValues.delete(this.values.length);
59+
lastValues.add(index);
60+
} else {
61+
this.values.pop();
62+
}
63+
if (!values.size) {
64+
this.map.delete(val);
65+
}
66+
return true;
67+
};
68+
69+
/**
70+
* @return {number}
71+
*/
72+
RandomizedCollection.prototype.getRandom = function() {
73+
return this.values[Math.floor(Math.random() * this.values.length)];
74+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@
299299
377|[Combination Sum IV](./0377-combination-sum-iv.js)|Medium|
300300
378|[Kth Smallest Element in a Sorted Matrix](./0378-kth-smallest-element-in-a-sorted-matrix.js)|Medium|
301301
380|[Insert Delete GetRandom O(1)](./0380-insert-delete-getrandom-o1.js)|Medium|
302+
381|[Insert Delete GetRandom O(1) - Duplicates allowed](./0381-insert-delete-getrandom-o1-duplicates-allowed.js)|Hard|
302303
383|[Ransom Note](./0383-ransom-note.js)|Easy|
303304
384|[Shuffle an Array](./0384-shuffle-an-array.js)|Medium|
304305
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|

0 commit comments

Comments
 (0)