All Posts

Leetcode 380: Insert Delete GetRandom O(1)

Design a data structure that supports three operations: insert, remove, and getRandom. The insert operation adds an element to the set if it is not already present. The remove operation removes an element from the set if it exists. The getRandom operation returns a random element from the set, with each element having an equal probability of being selected.

Leetcode 382: Linked List Random Node

Given a singly linked list, implement a class that supports the initialization of the list and the ability to return a random node’s value with equal probability.

Leetcode 384: Shuffle an Array

You are given an integer array nums. Design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.

Leetcode 398: Random Pick Index

Given an integer array nums with potential duplicates, you need to randomly select an index i where nums[i] == target. If there are multiple indices with the same value, the selection should be made with equal probability for each valid index.

Leetcode 497: Random Point in Non-overlapping Rectangles

You are given an array of non-overlapping axis-aligned rectangles. Each rectangle is represented by its bottom-left and top-right corner. Your task is to design a system that can pick a random integer point from the space covered by one of the given rectangles. A point on the perimeter of a rectangle is considered inside the rectangle.

Leetcode 519: Random Flip Matrix

You are given a binary grid of size m x n, where each cell is initially set to 0. The task is to design an algorithm that can randomly pick a cell that is still 0, flip it to 1, and ensure that all available cells are equally likely to be selected.