Tap here to switch tabs
Problem
Submissions
Solution

Solution: Insert Delete GetRandom O(1)

Statement

Naive approach

The naive approach to solve this problem is by using an array. Elements can be inserted and deleted from an array. Random values from an array can also be fetched by getting a random index first and then retrieving the array element.

The time complexity for this approach will be O(n)O(n), but our approach should have a running time of O(1) ...

Tap here to switch tabs
Problem
Submissions
Solution

Solution: Insert Delete GetRandom O(1)

Statement

Naive approach

The naive approach to solve this problem is by using an array. Elements can be inserted and deleted from an array. Random values from an array can also be fetched by getting a random index first and then retrieving the array element.

The time complexity for this approach will be O(n)O(n), but our approach should have a running time of O(1) ...