Problem
Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.
Note:
The array size can be very large. Solution that uses too much extra space will not pass the judge.
Example:
int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);
// pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(3);
// pick(1) should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(1);
class Solution { int[] nums; Random random; public Solution(int[] nums) { this.random = new Random(); this.nums = nums; } public int pick(int target) { int res = -1; int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != target) continue; else { count++; if (random.nextInt(count) == 0) res = i; } } return res; } } /* At first, let"s get clear that count is used to count the target number in nums. Say we now we have nums=[1,5,5,6,5,7,9,0] and the target is 5. Now let"s focus on the loop. When i=1, we get the first target number, and by rnd.nextInt(++count) we select a random number between [0, 1), which means actually you could only select 0, so the probability of making result = 1 is 1. Keep going. In the loop where i = 2, we get the second number. Now we have to get a random number in {0,1}. So what should we do if we want to keep result = 1? It"s simple: we have to make sure that, at this time, the random number generated should be 1 rather than 0 (otherwise the value of result will be changed), so the probability of keeping result = 1 is 1 * 1/2. It is similar when we get the third target number, i.e., i = 4. Now we have to get a random number in {0,1,2}. If we still wish to keep result = 1, the only way is to randomly get number 1 or 2 rather than 0, and the probability is 2/3. So the total probability of keeping result = 1 will be 1 * 1/2 * 2/3. Since we have four target number 5 here, the final probability of keeping result = 1 would be 1 * 1/2 * 2/3 * 3/4 = 1/4, which means the probability of picking index 0 is 1/4 as the problem required. The probability is the same if you wish to pick another index. You may ask what is the probability of picking the last possible index 4? Well, it simpler. You may ignore all operations before the loop where i = 4, and the only thing you have to do is to get the random number 0 among {0,1,2,3} in the loop where i = 4, so the probability is exactly 1/4. */
Reference: https://leetcode.com/problems...
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72311.html
摘要:思路一的實(shí)現(xiàn)其實(shí)要是想在的時(shí)間內(nèi)完成隨機(jī)數(shù)的獲取,只需要緩存每個(gè)數(shù)字出現(xiàn)的下標(biāo),但是這意味著需要先對數(shù)據(jù)進(jìn)行遍歷,并且還需要的空間來額外存儲數(shù)字下標(biāo)的集合。所以我們只能每次在獲取數(shù)字的時(shí)候來統(tǒng)計(jì)數(shù)字出現(xiàn)的次數(shù),然后針對次數(shù)獲取隨機(jī)數(shù)下標(biāo)。 題目要求 Given an array of integers with possible duplicates, randomly output ...
摘要:題目要求要求從單鏈表中,隨機(jī)返回一個(gè)節(jié)點(diǎn)的值,要求每個(gè)節(jié)點(diǎn)被選中的概率是相等的。假如一共有個(gè)物品,需要從其中挑選出個(gè)物品,要求確保個(gè)物品中每個(gè)物品都能夠被等概率選中。對于這種等概率問題,簡答的做法是通過隨機(jī)數(shù)獲取選中物品的下標(biāo)。 題目要求 Given a singly linked list, return a random nodes value from the linked li...
Problem 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
摘要:百分位數(shù)第百分位數(shù)是這樣一個(gè)值,它使得至少有的數(shù)據(jù)項(xiàng)小于或等于這個(gè)值,且至少有的數(shù)據(jù)項(xiàng)大于或等于這個(gè)值。即使極值變動(dòng)大,相比其他幾個(gè),還是比較接近實(shí)際數(shù)據(jù),曲線會有明顯變動(dòng),不像其他的一段時(shí)間可能都是平滑的。 基本概念 mean(平均值) 均值是就全部數(shù)據(jù)計(jì)算的,它具有優(yōu)良的數(shù)學(xué)性質(zhì),是實(shí)際中應(yīng)用最廣泛的集中趨勢測度值.其主要缺點(diǎn)是易受數(shù)據(jù)極端值的影響,對于偏態(tài)分布的數(shù)據(jù),均值的代表性...
摘要:本篇內(nèi)容將從鴨子類型的動(dòng)態(tài)協(xié)議,逐漸過渡到使接口更明確能驗(yàn)證實(shí)現(xiàn)是否符合規(guī)定的抽象基類。抽象基類介紹完動(dòng)態(tài)實(shí)現(xiàn)接口后,現(xiàn)在開始討論抽象基類,它屬于靜態(tài)顯示地實(shí)現(xiàn)接口。標(biāo)準(zhǔn)庫中的抽象基類從開始,標(biāo)準(zhǔn)庫提供了抽象基類。 《流暢的Python》筆記。本篇是面向?qū)ο髴T用方法的第四篇,主要討論接口。本篇內(nèi)容將從鴨子類型的動(dòng)態(tài)協(xié)議,逐漸過渡到使接口更明確、能驗(yàn)證實(shí)現(xiàn)是否符合規(guī)定的抽象基類(Abst...
閱讀 3057·2021-11-22 15:29
閱讀 1741·2021-10-12 10:11
閱讀 1776·2021-09-04 16:45
閱讀 2256·2021-08-25 09:39
閱讀 2801·2021-08-18 10:20
閱讀 2526·2021-08-11 11:17
閱讀 456·2019-08-30 12:49
閱讀 3318·2019-08-30 12:49