lc algorithm: random chapter

528. Randomly select by weight.
Given a positive integer array w, where w[i] represents the weight of subscript i (the subscript starts from 0), please write a function pickIndex, which can randomly obtain subscript i and select the next The probability of label i is proportional to w[i].
Method 1: Prefix sum + bisection

470. Use Rand7() to implement Rand10()
based on the API that generates 1-7 on average to implement the API
method of generating 1-10 on average: adopt a rejection algorithm in the form of a two-dimensional matrix based on the values ​​generated twice.

530. Shuffling arrays
Given an integer array nums, design an algorithm to shuffle an array without duplicate elements.
Implement Solution class:
Solution(int[] nums) Use integer array nums to initialize the object
int[] reset() reset the array to its initial state and return
int[] shuffle() return the result of randomly shuffling the array
Optimization and deletion ideas :Fisher-Yates
Insert image description here
519. Randomly flip the matrix
You are given an mxn binary matrix matrix, and all values ​​are initialized to 0. Please design an algorithm to randomly select a subscript (i, j) that satisfies matrix[i][j] == 0 and change its value to 1. All subscripts (i, j) that satisfy matrix[i][j] == 0 should have an equal probability of being selected.
Try to call the built-in random function as little as possible, and optimize the time and space complexity.
Implement the Solution class:
Solution(int m, int n) Initializes the object with the binary matrix size m and n
int[] flip() Returns a random subscript [i,] such that matrix[i][j] == 0 j] and change the value in the corresponding grid to 1
void reset() resets all the values ​​in the matrix to 0

Law: Fisher-Yates notes special verdict

497. Random points in non-overlapping rectangles
Given an array rects of non-overlapping axis-aligned rectangles, where rects[i] = [ai, bi, xi, yi] means (ai, bi) is the i-th rectangle The lower left corner point, (xi, yi) is the upper right corner point of the i-th rectangle. Design an algorithm to randomly pick an integer point covered by a certain rectangle. Points on the perimeter of the rectangle are also counted as covered by the rectangle. All points that meet the requirements must be returned with equal probability.
Any integer point within the space covered by the given rectangle may be returned.

Method: Either randomly sample in a large space, or serialize all points, and achieve the effect of random access points through random access to subscripts. It is more convenient to use the second method for this question. However, you cannot completely serialize all the points, because it will cause the array to be too large. A better way is to use the prefix sum method to number the points of each matrix, so that the point numbers of each matrix are fixed to consecutive numbers from x~y. , and then store each element of the array as the sum of the number of points of all previous matrices. This step can be divided into two to speed up. After finding the matrix where the point is located, you must also confirm the position of the point in the matrix. This step is very flexible. The lower left corner is the first element, the upper right corner is the last element, and the elements in the matrix are numbered from bottom to top -> from left to right. Thus solving this problem.
(Use prefix sum to solve the problem of excessive data volume, and use binary index element position. Although it is not as good as random reading O(1), the performance is good enough. In short, the matrix point numbering is used to solve this problem)

710. Random numbers in the blacklist
are the same as the shuffling algorithm. If rejection sampling is used, the speed will be poor. This question uses the mapping method. Unifiedly map all the numbers in the blacklist to the last consecutive number, and then randomly pick the previous segment each time to determine whether the map contains it, and return the mapped whitelist number if it does. This completely rejects sampling.

Guess you like

Origin blog.csdn.net/weixin_45719581/article/details/120115308