Skip to content

Commit e59844a

Browse files
committed
feat: add solutions to lc/lcof2 problem: Random Pick With Weight
1 parent b568e46 commit e59844a

File tree

11 files changed

+616
-5
lines changed

11 files changed

+616
-5
lines changed

lcof2/剑指 Offer II 071. 按权重生成随机数/README.md

+128-1
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,149 @@ solution.pickIndex(); // 返回 0,返回下标 0,返回该下标概率为 1/
7272

7373
<!-- 这里可写通用的实现逻辑 -->
7474

75+
“前缀和 + 二分查找”。
76+
7577
<!-- tabs:start -->
7678

7779
### **Python3**
7880

7981
<!-- 这里可写当前语言的特殊实现逻辑 -->
8082

8183
```python
82-
84+
class Solution:
85+
86+
def __init__(self, w: List[int]):
87+
n = len(w)
88+
self.presum = [0] * (n + 1)
89+
for i in range(n):
90+
self.presum[i + 1] = self.presum[i] + w[i]
91+
92+
def pickIndex(self) -> int:
93+
n = len(self.presum)
94+
x = random.randint(1, self.presum[-1])
95+
left, right = 0, n - 2
96+
while left < right:
97+
mid = (left + right) >> 1
98+
if self.presum[mid + 1] >= x:
99+
right = mid
100+
else:
101+
left = mid + 1
102+
return left
103+
104+
# Your Solution object will be instantiated and called as such:
105+
# obj = Solution(w)
106+
# param_1 = obj.pickIndex()
83107
```
84108

85109
### **Java**
86110

87111
<!-- 这里可写当前语言的特殊实现逻辑 -->
88112

89113
```java
114+
class Solution {
115+
private int[] presum;
116+
117+
public Solution(int[] w) {
118+
int n = w.length;
119+
presum = new int[n + 1];
120+
for (int i = 0; i < n; ++i) {
121+
presum[i + 1] = presum[i] + w[i];
122+
}
123+
}
124+
125+
public int pickIndex() {
126+
int n = presum.length;
127+
int x = (int) (Math.random() * presum[n - 1]) + 1;
128+
int left = 0, right = n - 2;
129+
while (left < right) {
130+
int mid = (left + right) >> 1;
131+
if (presum[mid + 1] >= x) {
132+
right = mid;
133+
} else {
134+
left = mid + 1;
135+
}
136+
}
137+
return left;
138+
}
139+
}
140+
141+
/**
142+
* Your Solution object will be instantiated and called as such:
143+
* Solution obj = new Solution(w);
144+
* int param_1 = obj.pickIndex();
145+
*/
146+
```
147+
148+
### **C++**
149+
150+
```cpp
151+
class Solution {
152+
public:
153+
vector<int> presum;
154+
155+
Solution(vector<int>& w) {
156+
int n = w.size();
157+
presum.resize(n + 1);
158+
for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + w[i];
159+
}
160+
161+
int pickIndex() {
162+
int n = presum.size();
163+
int x = rand() % presum[n - 1] + 1;
164+
int left = 0, right = n - 2;
165+
while (left < right)
166+
{
167+
int mid = left + right >> 1;
168+
if (presum[mid + 1] >= x) right = mid;
169+
else left = mid + 1;
170+
}
171+
return left;
172+
}
173+
};
174+
175+
/**
176+
* Your Solution object will be instantiated and called as such:
177+
* Solution* obj = new Solution(w);
178+
* int param_1 = obj->pickIndex();
179+
*/
180+
```
90181

182+
### **Go**
183+
184+
```go
185+
type Solution struct {
186+
presum []int
187+
}
188+
189+
func Constructor(w []int) Solution {
190+
n := len(w)
191+
pre := make([]int, n+1)
192+
for i := 0; i < n; i++ {
193+
pre[i+1] = pre[i] + w[i]
194+
}
195+
return Solution{pre}
196+
}
197+
198+
func (this *Solution) PickIndex() int {
199+
n := len(this.presum)
200+
x := rand.Intn(this.presum[n-1]) + 1
201+
left, right := 0, n-2
202+
for left < right {
203+
mid := (left + right) >> 1
204+
if this.presum[mid+1] >= x {
205+
right = mid
206+
} else {
207+
left = mid + 1
208+
}
209+
}
210+
return left
211+
}
212+
213+
/**
214+
* Your Solution object will be instantiated and called as such:
215+
* obj := Constructor(w);
216+
* param_1 := obj.PickIndex();
217+
*/
91218
```
92219

93220
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<int> presum;
4+
5+
Solution(vector<int>& w) {
6+
int n = w.size();
7+
presum.resize(n + 1);
8+
for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + w[i];
9+
}
10+
11+
int pickIndex() {
12+
int n = presum.size();
13+
int x = rand() % presum[n - 1] + 1;
14+
int left = 0, right = n - 2;
15+
while (left < right)
16+
{
17+
int mid = left + right >> 1;
18+
if (presum[mid + 1] >= x) right = mid;
19+
else left = mid + 1;
20+
}
21+
return left;
22+
}
23+
};
24+
25+
/**
26+
* Your Solution object will be instantiated and called as such:
27+
* Solution* obj = new Solution(w);
28+
* int param_1 = obj->pickIndex();
29+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
type Solution struct {
2+
presum []int
3+
}
4+
5+
func Constructor(w []int) Solution {
6+
n := len(w)
7+
pre := make([]int, n+1)
8+
for i := 0; i < n; i++ {
9+
pre[i+1] = pre[i] + w[i]
10+
}
11+
return Solution{pre}
12+
}
13+
14+
func (this *Solution) PickIndex() int {
15+
n := len(this.presum)
16+
x := rand.Intn(this.presum[n-1]) + 1
17+
left, right := 0, n-2
18+
for left < right {
19+
mid := (left + right) >> 1
20+
if this.presum[mid+1] >= x {
21+
right = mid
22+
} else {
23+
left = mid + 1
24+
}
25+
}
26+
return left
27+
}
28+
29+
/**
30+
* Your Solution object will be instantiated and called as such:
31+
* obj := Constructor(w);
32+
* param_1 := obj.PickIndex();
33+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
private int[] presum;
3+
4+
public Solution(int[] w) {
5+
int n = w.length;
6+
presum = new int[n + 1];
7+
for (int i = 0; i < n; ++i) {
8+
presum[i + 1] = presum[i] + w[i];
9+
}
10+
}
11+
12+
public int pickIndex() {
13+
int n = presum.length;
14+
int x = (int) (Math.random() * presum[n - 1]) + 1;
15+
int left = 0, right = n - 2;
16+
while (left < right) {
17+
int mid = (left + right) >> 1;
18+
if (presum[mid + 1] >= x) {
19+
right = mid;
20+
} else {
21+
left = mid + 1;
22+
}
23+
}
24+
return left;
25+
}
26+
}
27+
28+
/**
29+
* Your Solution object will be instantiated and called as such:
30+
* Solution obj = new Solution(w);
31+
* int param_1 = obj.pickIndex();
32+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
3+
def __init__(self, w: List[int]):
4+
n = len(w)
5+
self.presum = [0] * (n + 1)
6+
for i in range(n):
7+
self.presum[i + 1] = self.presum[i] + w[i]
8+
9+
def pickIndex(self) -> int:
10+
n = len(self.presum)
11+
x = random.randint(1, self.presum[-1])
12+
left, right = 0, n - 2
13+
while left < right:
14+
mid = (left + right) >> 1
15+
if self.presum[mid + 1] >= x:
16+
right = mid
17+
else:
18+
left = mid + 1
19+
return left
20+
21+
# Your Solution object will be instantiated and called as such:
22+
# obj = Solution(w)
23+
# param_1 = obj.pickIndex()

0 commit comments

Comments
 (0)