Skip to content

Commit 2c9f3c2

Browse files
committed
Two Problems Solutions - "Insert Delete GetRandom O(1)"
1 parent 9def962 commit 2c9f3c2

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ LeetCode
1515
|384|[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [C++](./algorithms/cpp/shuffleAnArray/ShuffleAnArray.cpp)|Medium|
1616
|383|[Ransom Note](https://leetcode.com/problems/ransom-note/) | [C++](./algorithms/cpp/ransomNote/RansomNote.cpp)|Easy|
1717
|382|[Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/) | [C++](./algorithms/cpp/linkedListRandomNode/LinkedListRandomNode.cpp)|Medium|
18+
|381|[Insert Delete GetRandom O(1) - Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/) | [C++](./algorithms/cpp/insertDeleteGetRandom/InsertDeleteGetrandomO1DuplicatesAllowed.cpp)|Hard|
19+
|380|[Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | [C++](./algorithms/cpp/insertDeleteGetRandom/InsertDeleteGetrandomO1.cpp)|Hard|
1820
|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | [C++](./algorithms/cpp/combinationSumIV/combinationSumIV.cpp)|Medium|
1921
|376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | [C++](./algorithms/cpp/wiggleSubsequence/wiggleSubsequence.cpp)|Medium|
2022
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [C++](./algorithms/cpp/intersectionOfTwoArraysII/intersectionOfTwoArraysII.cpp)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Source : https://leetcode.com/problems/insert-delete-getrandom-o1/
2+
// Author : Hao Chen
3+
// Date : 2016-08-25
4+
5+
/***************************************************************************************
6+
*
7+
* Design a data structure that supports all following operations in average O(1) time.
8+
*
9+
* insert(val): Inserts an item val to the set if not already present.
10+
* remove(val): Removes an item val from the set if present.
11+
* getRandom: Returns a random element from current set of elements. Each element must
12+
* have the same probability of being returned.
13+
*
14+
* Example:
15+
*
16+
* // Init an empty set.
17+
* RandomizedSet randomSet = new RandomizedSet();
18+
*
19+
* // Inserts 1 to the set. Returns true as 1 was inserted successfully.
20+
* randomSet.insert(1);
21+
*
22+
* // Returns false as 2 does not exist in the set.
23+
* randomSet.remove(2);
24+
*
25+
* // Inserts 2 to the set, returns true. Set now contains [1,2].
26+
* randomSet.insert(2);
27+
*
28+
* // getRandom should return either 1 or 2 randomly.
29+
* randomSet.getRandom();
30+
*
31+
* // Removes 1 from the set, returns true. Set now contains [2].
32+
* randomSet.remove(1);
33+
*
34+
* // 2 was already in the set, so return false.
35+
* randomSet.insert(2);
36+
*
37+
* // Since 1 is the only number in the set, getRandom always return 1.
38+
* randomSet.getRandom();
39+
***************************************************************************************/
40+
41+
42+
class RandomizedSet {
43+
public:
44+
/** Initialize your data structure here. */
45+
RandomizedSet() {
46+
srand(time(NULL));
47+
}
48+
49+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
50+
bool insert(int val) {
51+
if ( find(val) ) return false;
52+
data.push_back(val);
53+
valpos[val] = data.size() - 1;
54+
return true;
55+
}
56+
57+
/** Removes a value from the set. Returns true if the set contained the specified element. */
58+
bool remove(int val) {
59+
if ( !find(val) ) return false;
60+
61+
/*
62+
* Tricky
63+
* ------
64+
* 1) Copy the data from the last one to the place need be removed.
65+
* 2) Remove the last one.
66+
*/
67+
int _idx = valpos[val];
68+
int _val = data.back();
69+
70+
data[_idx] = _val;
71+
valpos[_val] = _idx;
72+
73+
valpos.erase(val);
74+
data.pop_back();
75+
return true;
76+
}
77+
78+
/** Get a random element from the set. */
79+
int getRandom() {
80+
return data[ rand() % data.size() ];
81+
}
82+
83+
private:
84+
unordered_map<int, int> valpos; //value position map
85+
vector<int> data;
86+
bool find(int val) {
87+
return (valpos.find(val) != valpos.end());
88+
}
89+
90+
};
91+
92+
/**
93+
* Your RandomizedSet object will be instantiated and called as such:
94+
* RandomizedSet obj = new RandomizedSet();
95+
* bool param_1 = obj.insert(val);
96+
* bool param_2 = obj.remove(val);
97+
* int param_3 = obj.getRandom();
98+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Source : https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/
2+
// Author : Hao Chen
3+
// Date : 2016-08-25
4+
5+
/***************************************************************************************
6+
*
7+
* Design a data structure that supports all following operations in average O(1) time.
8+
* Note: Duplicate elements are allowed.
9+
*
10+
* insert(val): Inserts an item val to the collection.
11+
* remove(val): Removes an item val from the collection if present.
12+
* getRandom: Returns a random element from current collection of elements. The
13+
* probability of each element being returned is linearly related to the number of same
14+
* value the collection contains.
15+
*
16+
* Example:
17+
*
18+
* // Init an empty collection.
19+
* RandomizedCollection collection = new RandomizedCollection();
20+
*
21+
* // Inserts 1 to the collection. Returns true as the collection did not contain 1.
22+
* collection.insert(1);
23+
*
24+
* // Inserts another 1 to the collection. Returns false as the collection contained 1.
25+
* Collection now contains [1,1].
26+
* collection.insert(1);
27+
*
28+
* // Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
29+
* collection.insert(2);
30+
*
31+
* // getRandom should return 1 with the probability 2/3, and returns 2 with the
32+
* probability 1/3.
33+
* collection.getRandom();
34+
*
35+
* // Removes 1 from the collection, returns true. Collection now contains [1,2].
36+
* collection.remove(1);
37+
*
38+
* // getRandom should return 1 and 2 both equally likely.
39+
* collection.getRandom();
40+
***************************************************************************************/
41+
42+
class RandomizedCollection {
43+
public:
44+
/** Initialize your data structure here. */
45+
RandomizedCollection() {
46+
srand(time(NULL));
47+
}
48+
49+
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
50+
bool insert(int val) {
51+
data.push_back(val);
52+
valpos[val].insert( data.size() - 1 );
53+
return (valpos[val].size() == 1);
54+
}
55+
56+
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
57+
bool remove(int val) {
58+
// not found
59+
if (!find(val)) return false;
60+
61+
62+
//same idea with non-duplication version, but need be careful with some edge case
63+
int _idx = *(valpos[val].begin());
64+
int _val = data.back();
65+
66+
valpos[_val].insert(_idx);
67+
data[_idx] = _val;
68+
69+
valpos[val].erase(_idx);
70+
if (valpos[val].size()==0){
71+
valpos.erase(val);
72+
}
73+
74+
data.pop_back();
75+
if ( _idx < data.size() ){
76+
valpos[_val].erase(data.size());
77+
valpos[_val].insert(_idx);
78+
}
79+
80+
return true;
81+
}
82+
83+
/** Get a random element from the collection. */
84+
int getRandom() {
85+
return data[ rand() % data.size() ];
86+
}
87+
private:
88+
unordered_map<int, unordered_set<int>> valpos; //value position map
89+
vector<int> data;
90+
bool find(int val) {
91+
return (valpos.find(val) != valpos.end());
92+
}
93+
94+
};
95+
96+
/**
97+
* Your RandomizedCollection object will be instantiated and called as such:
98+
* RandomizedCollection obj = new RandomizedCollection();
99+
* bool param_1 = obj.insert(val);
100+
* bool param_2 = obj.remove(val);
101+
* int param_3 = obj.getRandom();
102+
*/

0 commit comments

Comments
 (0)