成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 528. Random Pick with Weight

wangjuntytl / 1694人閱讀

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 <= w.length <= 10000
1 <= w[i] <= 10^5
pickIndex will be called at most 10000 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]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution"s constructor has one argument, the array w. pickIndex has no arguments. Arguments are always wrapped with a list, even if there aren"t any.

Solution

create an array sum, to save sum of weights

create a Random random to create random in a range

the range should be [1, maxValue] as [1, sum[len-1]]

use Random.nextInt(maxValue) + 1 to get a random num of the range

use binary search to find the index of random in sum array

class Solution {
    int[] sums;
    int max;
    Random random;
    public Solution(int[] w) {
        sums = new int[w.length];
        sums[0] = w[0];
        for (int i = 1; i < sums.length; i++) {
            sums[i] = sums[i-1]+w[i];
        }
        max = sums[sums.length-1];
        random = new Random();
    }
    
    public int pickIndex() {
        int rand = random.nextInt(max)+1;
        int index = findIndex(sums, rand);
        return index;
    }
    
    private int findIndex(int[] nums, int k) {
        int start = 0, end = nums.length-1;
        while (start+1 < end) {
            int mid = start+(end-start)/2;
            if (nums[mid] == k) return mid;
            else if (nums[mid] < k) start = mid+1;
            else end = mid-1;
        }
        if (k > nums[end]) return end+1;
        else if (k > nums[start]) return start+1;
        else return start;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72117.html

相關(guān)文章

  • [LeetCode] 398. Random Pick Index (Reservoir Sampl

    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 siz...

    edagarli 評論0 收藏0
  • leetcode398. Random Pick Index

    摘要:思路一的實(shí)現(xiàn)其實(shí)要是想在的時(shí)間內(nèi)完成隨機(jī)數(shù)的獲取,只需要緩存每個(gè)數(shù)字出現(xiàn)的下標(biāo),但是這意味著需要先對數(shù)據(jù)進(jìn)行遍歷,并且還需要的空間來額外存儲(chǔ)數(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 ...

    airborne007 評論0 收藏0
  • leetcode382. Linked List Random Node

    摘要:題目要求要求從單鏈表中,隨機(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...

    xiaodao 評論0 收藏0
  • 三維重建工具——pclpy教程之八叉樹的空間分區(qū)和搜索操作

    摘要:在本教程中,我們將學(xué)習(xí)如何使用八叉樹在點(diǎn)云數(shù)據(jù)中進(jìn)行空間分區(qū)和鄰居搜索。因此,搜索點(diǎn)和搜索結(jié)果之間的距離取決于八叉樹的分辨率參數(shù)。此外,先進(jìn)的內(nèi)存管理減少了八叉樹構(gòu)建過程中的內(nèi)存分配和釋放操作??偨Y(jié)八叉樹實(shí)現(xiàn)是空間分區(qū)和搜索操作的強(qiáng)大工具。 本教程代碼開源:GitHub 歡迎st...

    番茄西紅柿 評論0 收藏2637
  • [LintCode/LeetCode] Copy List with Random Pointer

    摘要:大體意思就是,先復(fù)制到,順便將所有的放在再復(fù)制所有的到,順便將所有的放在最后令,令,將和分離,返回的頭結(jié)點(diǎn) Problem A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. ...

    Jacendfeng 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<