Skip to content

Commit 94e56b5

Browse files
committed
380. Insert Delete GetRandom O(1)
1 parent 6d08f5e commit 94e56b5

File tree

3 files changed

+148
-28
lines changed

3 files changed

+148
-28
lines changed

README.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
# LeetCode
22

3-
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-27%20%2F%203008-1f425f?logo=leetcode)
3+
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-28%20%2F%203008-1f425f?logo=leetcode)
44
![Language: Golang](https://img.shields.io/badge/language-Golang-00ADD8?logo=go)
55
![Language: Rust](https://img.shields.io/badge/language-Rust-00ADD8?logo=rust)
66
![Language: Bash](https://img.shields.io/badge/language-Bash-00ADD8?logo=gnubash&logoColor=f5f5f5)
77

88
### LeetCode Algorithm
99

10-
| # | Title | Solution | Difficulty | Topics |
11-
|----:|---------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------|:----------:|:----------------------------------:|
12-
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Go](go/0001_two_sum) | 🟢 | Array, Hash Table |
13-
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Go](go/0002_add_two_numbers) | 🟡 | Linked List, Math, Recursion |
14-
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Go](go/0003_longest_substring) | 🟡 | Hash Table, String, Sliding Window |
15-
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [Go](go/0007_reverse_integer) | 🟡 | Math |
16-
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Go](go/0009_palindrome_number) | 🟢 | Math |
17-
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [Go](go/0013_roman_to_integer) | 🟢 | Hash Table, Math, String |
18-
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [Go](go/0017_letter_combinations_phone) | 🟡 | Hash Table, String, Backtracking |
19-
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Go](go/0020_valid_parentheses) | 🟢 | String, Stack |
20-
| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Go](go/0026_remove_duplicates_from_array) | 🟢 | Array, Pointer |
21-
| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Go](go/0035_search_insert_position), [Rust](rust/_0035_search_insert_position) | 🟢 | Array, Binary Search |
22-
| 50 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Go](go/0050_powx_n) | 🟡 | Math, Recursion |
23-
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Go](go/0069_sqrtx), [Rust](rust/_0069_sqrtx) | 🟢 | Math, Binary Search |
24-
| 75 | [Sort Colors](https://leetcode.com/problems/sort-colors/) | [Go](go/0075_sort_colors) | 🟡 | Array, Pointer, Sorting |
25-
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Go](go/0088_merge_sorted_array) | 🟢 | Array, Pointer |
26-
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Go](go/0125_valid_palindrome) | 🟢 | String, Pointer |
27-
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Go](go/0136_single_number) | 🟢 | Array, Bit Manipulation |
28-
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Go](go/0155_min_stack) | 🟢 | Stack |
29-
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Go](go/0217_contains_duplicate) | 🟢 | Array, Hash Table |
30-
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Go](go/0226_invert_binary_tree) | 🟢 | Tree, BFS, DFS, Binary Tree |
31-
| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Go](go/0268_missing_number) | 🟢 | Array, Math, Bit Manipulation |
32-
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [Go](go/0283_move_zeroes) | 🟢 | Array, Pointer |
33-
| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Go](go/0344_reverse_string), [Rust](rust/_0344_reverse_string) | 🟢 | String, Pointer |
34-
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Go](go/0404_sum_of_left_leaves) | 🟢 | Tree, BFS, DFS, Binary Tree |
35-
| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [Rust](rust/_0540_single_elem_in_array) | 🟡 | Array, Binary Search |
36-
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Go](go/0704_binary_search), [Rust](rust/_0704_binary_search) | 🟢 | Array, Binary Search |
10+
| # | Title | Solution | Difficulty | Topics |
11+
|----:|---------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------|:----------:|:------------------------------------:|
12+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Go](go/0001_two_sum) | 🟢 | Array, Hash Table |
13+
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [Go](go/0002_add_two_numbers) | 🟡 | Linked List, Math, Recursion |
14+
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Go](go/0003_longest_substring) | 🟡 | Hash Table, String, Sliding Window |
15+
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [Go](go/0007_reverse_integer) | 🟡 | Math |
16+
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Go](go/0009_palindrome_number) | 🟢 | Math |
17+
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [Go](go/0013_roman_to_integer) | 🟢 | Hash Table, Math, String |
18+
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [Go](go/0017_letter_combinations_phone) | 🟡 | Hash Table, String, Backtracking |
19+
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Go](go/0020_valid_parentheses) | 🟢 | String, Stack |
20+
| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Go](go/0026_remove_duplicates_from_array) | 🟢 | Array, Pointer |
21+
| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Go](go/0035_search_insert_position), [Rust](rust/_0035_search_insert_position) | 🟢 | Array, Binary Search |
22+
| 50 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Go](go/0050_powx_n) | 🟡 | Math, Recursion |
23+
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Go](go/0069_sqrtx), [Rust](rust/_0069_sqrtx) | 🟢 | Math, Binary Search |
24+
| 75 | [Sort Colors](https://leetcode.com/problems/sort-colors/) | [Go](go/0075_sort_colors) | 🟡 | Array, Pointer, Sorting |
25+
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [Go](go/0088_merge_sorted_array) | 🟢 | Array, Pointer |
26+
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Go](go/0125_valid_palindrome) | 🟢 | String, Pointer |
27+
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [Go](go/0136_single_number) | 🟢 | Array, Bit Manipulation |
28+
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Go](go/0155_min_stack) | 🟢 | Stack |
29+
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Go](go/0217_contains_duplicate) | 🟢 | Array, Hash Table |
30+
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Go](go/0226_invert_binary_tree) | 🟢 | Tree, BFS, DFS, Binary Tree |
31+
| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Go](go/0268_missing_number) | 🟢 | Array, Math, Bit Manipulation |
32+
| 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [Go](go/0283_move_zeroes) | 🟢 | Array, Pointer |
33+
| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Go](go/0344_reverse_string), [Rust](rust/_0344_reverse_string) | 🟢 | String, Pointer |
34+
| 380 | [Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/) | [Go](go/0380_insert_delete_getrandom_o1) | 🟡 | Array, Hash Table, Math, Randomized |
35+
| 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [Go](go/0404_sum_of_left_leaves) | 🟢 | Tree, BFS, DFS, Binary Tree |
36+
| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [Rust](rust/_0540_single_elem_in_array) | 🟡 | Array, Binary Search |
37+
| 704 | [Binary Search](https://leetcode.com/problems/binary-search/) | [Go](go/0704_binary_search), [Rust](rust/_0704_binary_search) | 🟢 | Array, Binary Search |
3738

3839
### LeetCode Shell
3940

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package insert_delete_getrandom_o1
2+
3+
import "math/rand"
4+
5+
type RandomizedSet struct {
6+
items []int
7+
table map[int]int
8+
}
9+
10+
func Constructor() RandomizedSet {
11+
const initSize = 25
12+
13+
return RandomizedSet{
14+
items: make([]int, 0, initSize),
15+
table: make(map[int]int, initSize),
16+
}
17+
}
18+
19+
func (rs *RandomizedSet) Insert(val int) bool {
20+
if _, found := rs.table[val]; found {
21+
return false
22+
}
23+
24+
rs.items = append(rs.items, val)
25+
rs.table[val] = len(rs.items) - 1
26+
27+
return true
28+
}
29+
30+
func (rs *RandomizedSet) Remove(val int) bool {
31+
idx, found := rs.table[val]
32+
if !found {
33+
return false
34+
}
35+
36+
delete(rs.table, val) // Remove val element from map
37+
rs.items[idx] = rs.items[len(rs.items)-1] // Swap val with last element in array
38+
rs.items[len(rs.items)-1] = 0 // Avoid memory leak
39+
rs.items = rs.items[:len(rs.items)-1] // Remove last element from array
40+
41+
if idx < len(rs.items) {
42+
rs.table[rs.items[idx]] = idx // Update index for moved last element in map
43+
}
44+
45+
return true
46+
}
47+
48+
func (rs *RandomizedSet) GetRandom() int {
49+
randIdx := rand.Intn(len(rs.items))
50+
51+
return rs.items[randIdx]
52+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package insert_delete_getrandom_o1
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_SuccessCase1(t *testing.T) {
10+
randomizedSet := Constructor()
11+
12+
assert.True(t, randomizedSet.Insert(1)) // Inserts 1 to the set. Returns true as 1 was inserted successfully
13+
assert.False(t, randomizedSet.Remove(2)) // Returns false as 2 does not exist in the set
14+
assert.True(t, randomizedSet.Insert(2)) // Inserts 2 to the set, returns true. Set now contains [1,2]
15+
16+
assert.Contains(t, []int{1, 2}, randomizedSet.GetRandom()) // getRandom() should return either 1 or 2 randomly
17+
18+
assert.True(t, randomizedSet.Remove(1)) // Removes 1 from the set, returns true. Set now contains [2]
19+
assert.False(t, randomizedSet.Insert(2)) // 2 was already in the set, so return false
20+
21+
assert.Equal(t, 2, randomizedSet.GetRandom()) // Since 2 is the only number in the set, getRandom() will always return 2
22+
}
23+
24+
func Test_SuccessCase2(t *testing.T) {
25+
randomizedSet := Constructor()
26+
27+
assert.True(t, randomizedSet.Insert(0))
28+
assert.True(t, randomizedSet.Insert(1))
29+
assert.True(t, randomizedSet.Remove(0))
30+
31+
assert.True(t, randomizedSet.Insert(2))
32+
assert.True(t, randomizedSet.Remove(1))
33+
34+
assert.Equal(t, 2, randomizedSet.GetRandom())
35+
}
36+
37+
func Test_SuccessCase3(t *testing.T) {
38+
randomizedSet := Constructor()
39+
40+
assert.False(t, randomizedSet.Remove(0))
41+
assert.False(t, randomizedSet.Remove(0))
42+
assert.True(t, randomizedSet.Insert(0))
43+
44+
assert.Equal(t, 0, randomizedSet.GetRandom())
45+
46+
assert.True(t, randomizedSet.Remove(0))
47+
assert.True(t, randomizedSet.Insert(0))
48+
}
49+
50+
func Test_SuccessCase4(t *testing.T) {
51+
randomizedSet := Constructor()
52+
53+
assert.True(t, randomizedSet.Insert(3))
54+
assert.False(t, randomizedSet.Insert(3))
55+
56+
assert.Equal(t, 3, randomizedSet.GetRandom())
57+
assert.Equal(t, 3, randomizedSet.GetRandom())
58+
59+
assert.True(t, randomizedSet.Insert(1))
60+
assert.True(t, randomizedSet.Remove(3))
61+
62+
assert.Equal(t, 1, randomizedSet.GetRandom())
63+
assert.Equal(t, 1, randomizedSet.GetRandom())
64+
65+
assert.True(t, randomizedSet.Insert(0))
66+
assert.True(t, randomizedSet.Remove(0))
67+
}

0 commit comments

Comments
 (0)