LeetCode Random常见的题型以及要注意的点
710 Random Pick with Blacklist 32.5% Hard uniformly pick - 1D remapping
470 Implement Rand10() Using Rand7() 45.9% Medium uniformly pick
528 Random Pick with Weight 44.4% Medium not uniform pick(by weight) -> using prefix sum array and binary search
519 Random Flip Matrix 37.5% Medium uniformly pick -2D remapping
497 Random Point in Non-overlapping Rectangles 39.0% Medium uniform
478 Generate Random Point in a Circle 38.8% Medium reject sampling
380/381 Insert Delete GetRandom O(1) - use hashmap and arraylist, use pseudo delete(swap with the end)
138 Copy List with Random Pointer --this is actually not a random related problem, it’s actually a deep copy problem
1485 Clone Binary Tree With Random Pointer ----this is actually not a random related problem, it’s actually a deep copy problem
398 Random Pick Index – reservoir sampling problem
382 Linked List Random Node --very easy, just randomly pick a index. but it can also can be solved in reservoir sampling algorithm
核心函数:
Math.random()
Random rand = new Random();
rand.nextInt()
总体来说 random相关的问题还是非常简单的 只是有些时候需要一些小技巧而已,比如说remapping, prefix sum array, reject sampling(均匀随机的在一个大空间内取点意味着在其中任何一个子空间取点都是均匀随机的), reservoir sampling algorithm等等。

浙公网安备 33010602011771号