Рет қаралды 33,607
This video explains a very tricky and important interview question which is to randomly pick index based on weighted value.In this problem, we are provided with an array of weights and we are required to choose an index in such a way that the probability of getting an index chosen is higher for a higher weight index and lower for a lower weight index.I have explained the approach to solve this problem using bucket.Once we form buckets then the next step is to just generate the random number for each query in the range of 0 to SUM and use binary search to find in which bucket does the random number fall into.We can also use upper_bound to avoid implementing binary search.I have explained the code walkthrough at the end.CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
=================================================================
INSTAGRAM: / surya.pratap.k
LinkedIn: / surya-pratap-kahar-47b...
=================================================================
CODE LINK: gist.github.co...
SIMILAR PROBLEM:-
Aggressive cow: • Aggressive cow | SPOJ