forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Search] Add a solution to Random Pick with Weight
- Loading branch information
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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() | ||
*/ |