【LeetCode】隨機化演算法 random(共6題)
【470】Implement Rand10() Using Rand7() (2018年11月15日,新學的演算法)
給了一個現成的api rand7(),這個介面能產生 [1,7] 區間的隨機數。根據這個api,寫一個 rand10() 的演算法生成 [1, 10] 區間隨機數。
題解:這個題《程式設計師程式碼面試指南》上講了這題。我粗淺的描述一下產生過程:
(1)rand7() 等概率的產生 1,2, 3, 4, 5, 6,7.
(2)rand7()-1 等概率的產生 [0, 6]
(3)(rand7() - 1) * 7 等概率的產生 0, 7, 14, 21, 28, 35, 42
(4)(rand7() - 1) * 7 + (rand7() - 1)等概率的產生 [0, 48] 這49個數字
(5)如果步驟4的結果大於等於40,那麼就重複步驟4,直到產生的數小於40.
(6)把步驟5的結果mod 10再加1,就會等概率的隨機生成[1, 10].
總之,公式是 (randX() - 1) * X + (randX() - 1)。
1 // The rand7() API is already defined for you. 2 // int rand7();View Code3 // @return a random integer in the range 1 to 7 4 5 class Solution { 6 public: 7 int rand10() { 8 int num = 0; 9 do { 10 num = (rand7()-1) * 7 + (rand7()-1); 11 } while(num >= 40); 12 return num % 10 + 1; 13 } 14 };
本題還有兩個follow-up:
-
What is the expected value for the number of calls to
rand7()
function? -
Could you minimize the number of calls to
rand7()
?
《程式設計師程式碼面試指南》後面的進階演算法還沒看,chp 9, P391
【478】Generate Random Point in a Circle
【497】Random Point in Non-overlapping Rectangles
【519】Random Flip Matrix
【528】Random Pick with Weight (2018年12月31日,昨天演算法群 mock 原題)
mock相關連結:https://www.cnblogs.com/zhangwanying/p/10199941.html (第一場-第四題)
Given an array w
of positive integers, where w[i]
describes the weight of index i
, write a function pickIndex
which randomly picks an index in proportion to its weight.
Note:
1 <= w.length <= 10000
1 <= w[i] <= 10^5
pickIndex
will be called at most10000
times.
Example 1: Input: ["Solution","pickIndex"] [[[1]],[]] Output: [null,0] Example 2: Input: ["Solution","pickIndex","pickIndex","pickIndex","pickIndex","pickIndex"] [[[1,3]],[],[],[],[],[]] Output: [null,0,1,1,1,0]
題解:把 weight 陣列求字首和,然後隨機出一個在區間 [0, tot) 中的隨機數,然後在字首和陣列中二分判斷index。
1 class Solution { 2 public: 3 Solution(vector<int> w) { 4 const int n = w.size(); 5 vector<int> summ2(n+1, 0); 6 for (int i = 1; i <= n; ++i) { 7 summ2[i] = w[i-1] + summ2[i-1]; 8 } 9 summ = summ2; 10 } 11 12 int pickIndex() { 13 int tot = summ.back(); 14 int r = (rand() % tot) + 1; 15 auto iter = lower_bound(summ.begin(), summ.end(), r); 16 int ret = distance(summ.begin(), iter) - 1; 17 return ret; 18 } 19 vector<int> summ; 20 }; 21 22 /** 23 * Your Solution object will be instantiated and called as such: 24 * Solution obj = new Solution(w); 25 * int param_1 = obj.pickIndex(); 26 */View Code
【710】Random Pick with Blacklist