Skip to content

Commit d3e7ffd

Browse files
committed
problem: new problem solution - 380 . Insert Delete GetRandom O(1)
1 parent efd0f42 commit d3e7ffd

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ My approach to solving LeetCode problems typically involves the following steps:
164164
| 3005 | [Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency/) | Algorithms | [TypeScript](./problems/algorithms/countElementsWithMaximumFrequency/CountElementsWithMaximumFrequency.ts) | Easy |
165165
| 1657 | [Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/) | Algorithms | [TypeScript](./problems/algorithms/determineIfTwoStringsAreClose/DetermineIfTwoStringsAreClose.ts) | Medium |
166166
| 2225 | [Find Players With Zero or One Losses](https://leetcode.com/problems/find-players-with-zero-or-one-losses/) | Algorithms | [TypeScript](./problems/algorithms/findPlayersWithZeroOrOneLosses/FindPlayersWithZeroOrOneLosses.ts) | Medium |
167+
| 380 | [Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | Algorithms | [TypeScript](./problems/algorithms/insertDeleteGetrandomO1/InsertDeleteGetrandomO1.ts) | Medium |
167168
| ... | ... | ... | ... | ... |
168169

169170
In this table:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Source : https://leetcode.com/problems/insert-delete-getrandom-o1/
2+
// Author : francisco
3+
// Date : 2024-01-16
4+
5+
import { RandomizedSet } from "./InsertDeleteGetrandomO1";
6+
7+
describe("Insert Delete GetRandom O(1)", () => {
8+
test("example 1", () => {
9+
const randomizedSet: RandomizedSet = new RandomizedSet();
10+
11+
expect(randomizedSet.insert(1)).toBe(true);
12+
expect(randomizedSet.remove(2)).toBe(false);
13+
expect(randomizedSet.insert(2)).toBe(true);
14+
expect(randomizedSet.remove(1)).toBe(true);
15+
expect(randomizedSet.insert(2)).toBe(false);
16+
expect(randomizedSet.getRandom()).toBe(2);
17+
});
18+
});
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Source : https://leetcode.com/problems/insert-delete-getrandom-o1/
2+
// Author : francisco
3+
// Date : 2024-01-16
4+
5+
/*****************************************************************************************************
6+
*
7+
* Implement the RandomizedSet class:
8+
*
9+
* RandomizedSet() Initializes the RandomizedSet object.
10+
* bool insert(int val) Inserts an item val into the set if not present. Returns true if the
11+
* item was not present, false otherwise.
12+
* bool remove(int val) Removes an item val from the set if present. Returns true if the item
13+
* was present, false otherwise.
14+
* int getRandom() Returns a random element from the current set of elements (it's guaranteed
15+
* that at least one element exists when this method is called). Each element must have the same
16+
* probability of being returned.
17+
*
18+
* You must implement the functions of the class such that each function works in average O(1) time
19+
* complexity.
20+
*
21+
* Example 1:
22+
*
23+
* Input
24+
* ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
25+
* [[], [1], [2], [2], [], [1], [2], []]
26+
* Output
27+
* [null, true, false, true, 2, true, false, 2]
28+
*
29+
* Explanation
30+
* RandomizedSet randomizedSet = new RandomizedSet();
31+
* randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
32+
* randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
33+
* randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
34+
* randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
35+
* randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
36+
* randomizedSet.insert(2); // 2 was already in the set, so return false.
37+
* randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return
38+
* 2.
39+
*
40+
* Constraints:
41+
*
42+
* -2^31 <= val <= 2^31 - 1
43+
* At most 2 * 10^5 calls will be made to insert, remove, and getRandom.
44+
* There will be at least one element in the data structure when getRandom is called.
45+
******************************************************************************************************/
46+
47+
export class RandomizedSet {
48+
list: number[];
49+
map: Map<number, number>;
50+
51+
constructor() {
52+
this.list = [];
53+
this.map = new Map();
54+
}
55+
56+
search(val: number): boolean {
57+
return this.map.has(val);
58+
}
59+
60+
insert(val: number): boolean {
61+
if (this.search(val)) return false;
62+
63+
this.list.push(val);
64+
this.map.set(val, this.list.length - 1);
65+
66+
return true;
67+
}
68+
69+
remove(val: number): boolean {
70+
if (!this.search(val)) return false;
71+
72+
const index: number = this.map.get(val) as number;
73+
74+
this.list[index] = this.list[this.list.length - 1] as number;
75+
this.map.set(this.list[index] as number, index);
76+
this.list.pop();
77+
this.map.delete(val);
78+
79+
return true;
80+
}
81+
82+
getRandom(): number {
83+
const randomIndex: number = Math.floor(Math.random() * this.list.length);
84+
85+
return this.list[randomIndex] as number;
86+
}
87+
}
88+
89+
/**
90+
* Your RandomizedSet object will be instantiated and called as such:
91+
* var obj = new RandomizedSet()
92+
* var param_1 = obj.insert(val)
93+
* var param_2 = obj.remove(val)
94+
* var param_3 = obj.getRandom()
95+
*/

0 commit comments

Comments
 (0)