Skip to content

Commit

Permalink
[Search] Add a solution to Random Pick with Weight
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jan 1, 2020
1 parent 5a1cbe9 commit 7efda15
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 314 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 315 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.

## Contributors

Expand Down Expand Up @@ -356,6 +356,7 @@
[Search Insert Position](https://leetcode.com/problems/search-insert-position/)| [Swift](./Search/SearchForARange.swift)| Medium| O(logn)| O(1)|
[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)| [Swift](./Search/PeakIndexMountainArray.swift)| Easy| O(logn)| O(1)|
[Find Peak Element](https://leetcode.com/problems/find-peak-element/)| [Swift](./Search/FindPeakElement.swift)| Medium| O(logn)| O(1)|
[Random Pick with Weight](https://leetcode.com/problems/random-pick-with-weight/)| [Swift](./Search/RandomPickWeight.swift)| Medium| O(logn)| O(1)|
[Sqrt(x)](https://leetcode.com/problems/sqrtx/)| [Swift](./Search/Sqrtx.swift)| Medium| O(logn)| O(1)|
[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)| [Swift](./Search/MedianTwoSortedArrays.swift)| Hard| O(log(m + n))| O(1)|
[Minimize Max Distance to Gas Station](https://leetcode.com/problems/minimize-max-distance-to-gas-station/)| [Swift](./Search/MinimizeMaxDistanceGasStation.swift)| Hard| O(nlogm)| O(1)|
Expand Down
50 changes: 50 additions & 0 deletions Search/RandomPickWeight.swift
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()
*/

0 comments on commit 7efda15

Please sign in to comment.