Skip to content

Commit a904e6e

Browse files
author
Partho Biswas
committed
380. Insert Delete GetRandom O(1)
1 parent 4b67b63 commit a904e6e

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ I have solved quite a number of problems from several topics. See the below tabl
156156
|48| **[362. Design Hit Counter](https://tinyurl.com/sswrcc3)** | [Python](https://tinyurl.com/wu6rdaw/362_Design_Hit_Counter.py), [Swift](https://tinyurl.com/wuja3c4/362_Design_Hit_Counter.swift)| --- | Medium | --- |
157157
|49| **[289. Game of Life](https://tinyurl.com/y5ujyvu5)** | [Python](https://tinyurl.com/wu6rdaw/289_Game_of_Life.py), [Swift](https://tinyurl.com/wuja3c4/289_Game_of_Life.swift)| --- | Medium | --- |
158158
|50| **[54. Spiral Matrix](https://tinyurl.com/yy5rnvce)** | [Python](https://tinyurl.com/wu6rdaw/54_Spiral_Matrix.py), [Swift](https://tinyurl.com/wuja3c4/54_Spiral_Matrix.swift)| [Official](https://tinyurl.com/s6np53k) | Medium | --- |
159+
|51| **[380. Insert Delete GetRandom O(1)](https://tinyurl.com/y3urnkfj)** | [Python](https://tinyurl.com/wu6rdaw/380_Insert_Delete_GetRandom_O(1).py), [Swift](https://tinyurl.com/wuja3c4/380_Insert_Delete_GetRandom_O(1).swift)| [Official](https://tinyurl.com/t4t38od) | Medium | --- |
159160

160161

161162
</p>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import random
2+
class RandomizedSet(object):
3+
4+
def __init__(self):
5+
"""
6+
Initialize your data structure here.
7+
"""
8+
self.valueIdxMap = {}
9+
self.valueList = []
10+
11+
def insert(self, val):
12+
"""
13+
Inserts a value to the set. Returns true if the set did not already contain the specified element.
14+
:type val: int
15+
:rtype: bool
16+
"""
17+
if val in self.valueIdxMap:
18+
return False
19+
self.valueIdxMap[val] = len(self.valueList)
20+
self.valueList.append(val)
21+
return True
22+
23+
def remove(self, val):
24+
"""
25+
Removes a value from the set. Returns true if the set contained the specified element.
26+
:type val: int
27+
:rtype: bool
28+
"""
29+
if val in self.valueIdxMap:
30+
idx = self.valueIdxMap[val]
31+
lastVal = self.valueList[-1]
32+
self.valueList[idx] = lastVal
33+
self.valueIdxMap[lastVal] = idx
34+
self.valueList.pop()
35+
del self.valueIdxMap[val]
36+
return True
37+
return False
38+
39+
def getRandom(self):
40+
"""
41+
Get a random element from the set.
42+
:rtype: int
43+
"""
44+
return random.choice(self.valueList)
45+
46+
# Your RandomizedSet object will be instantiated and called as such:
47+
# obj = RandomizedSet()
48+
# param_1 = obj.insert(val)
49+
# param_2 = obj.remove(val)
50+
# param_3 = obj.getRandom()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Foundation
2+
3+
4+
class RandomizedSet {
5+
var valueIdxMap = [Int:Int]()
6+
var valueList = [Int]()
7+
8+
/** Initialize your data structure here. */
9+
init() {
10+
11+
}
12+
13+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
14+
func insert(_ val: Int) -> Bool {
15+
guard self.valueIdxMap[val] == nil else {
16+
return false
17+
}
18+
self.valueIdxMap[val] = self.valueList.count
19+
self.valueList.append(val)
20+
return true
21+
}
22+
23+
/** Removes a value from the set. Returns true if the set contained the specified element. */
24+
func remove(_ val: Int) -> Bool {
25+
guard self.valueIdxMap[val] != nil else {
26+
return false
27+
}
28+
let idx = self.valueIdxMap[val]
29+
let lastVal = self.valueList.last
30+
self.valueList[idx!] = lastVal!
31+
self.valueIdxMap[lastVal!] = idx
32+
self.valueList.popLast()
33+
self.valueIdxMap.removeValue(forKey: val)
34+
return true
35+
}
36+
37+
/** Get a random element from the set. */
38+
func getRandom() -> Int {
39+
var idx = Int.random(in: 0..<self.valueList.count)
40+
return self.valueList[idx]
41+
}
42+
}
43+
44+
/**
45+
* Your RandomizedSet object will be instantiated and called as such:
46+
* let obj = RandomizedSet()
47+
* let ret_1: Bool = obj.insert(val)
48+
* let ret_2: Bool = obj.remove(val)
49+
* let ret_3: Int = obj.getRandom()
50+
*/

0 commit comments

Comments
 (0)