Insert Delete GetRandom O(1) Explained | LeetCode 380 C#
Solution Quest
0:00 / 0:00
Insert Delete GetRandom O(1) Explained | LeetCode 380 C#
2 просмотра · 4 дня назад
Solution Quest
2 просмотра · 4 дня назад
Master LeetCode 380, Insert Delete GetRandom O(1), with a visual C# walkthrough of RandomizedSet.
Learn how a List plus Dictionary enables uniform random selection, and how the swap-with-last trick removes any value without shifting elements.
In this episode:
Define the RandomizedSet contract
Combine a dense list with a value-to-index dictionary
Implement average O(1) Insert and GetRandom
See why removing directly from the middle is O(n)
Derive swap-with-last removal step by step
Walk through the official LeetCode example
Read the complete C# implementation
Review time and space complexity
Chapters:
0:00 The RandomizedSet goal
0:27 Insert, Remove, and GetRandom contract
0:59 One set, two data structures
1:34 Insert in average O(1)
2:10 Uniform GetRandom
2:46 Pause and solve the removal challenge
3:21 Why RemoveAt(index) is too slow
3:49 Swap-with-last walkthrough
4:28 Why changing order is safe
4:57 Official LeetCode example
5:40 C# state and Insert
6:09 C# Remove
6:45 C# GetRandom and complexity
7:13 The pattern to remember
Recall challenge: after copying the last value into the removed value's slot, which mapping must be repaired?
#LeetCode #CSharp #DataStructures
Tags
leetcode 380, insert delete getrandom o1, insert delete getrandom o(1), randomizedset, randomized set c#, leetcode c#, leetcode explained, data structures, hash table, dictionary c#, list c#, swap with last, coding interview, top interview 150, solution quest