forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomPickWeight.swift
50 lines (39 loc) · 1.17 KB
/
RandomPickWeight.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Question Link: https://leetcode.com/problems/random-pick-with-weight/
* Primary idea: Random select a number from sum of the array, and search the first number
* greater than the number in sums array constructed from the original array.
*
* Time Complexity: O(logn), Space Complexity: O(n)
*/
class RandomPickWeight {
var sums: [Int]
init(_ w: [Int]) {
sums = w
for i in 1..<w.count {
sums[i] += sums[i - 1]
}
}
func pickIndex() -> Int {
guard let sum = sums.last else {
return -1
}
return findFirstGreaterThan(Int.random(in: 0..<sum))
}
private func findFirstGreaterThan(_ num: Int) -> Int {
var left = 0, right = sums.count - 1
while left < right {
let mid = (right - left) / 2 + left
if sums[mid] > num {
right = mid
} else {
left = mid + 1
}
}
return left
}
}
/**
* Your Solution object will be instantiated and called as such:
* let obj = Solution(w)
* let ret_1: Int = obj.pickIndex()
*/