Skip to content

Commit 602738b

Browse files
committed
feat: add solutions to lc problem: No.0528
No.0528.Random Pick with Weight
1 parent 77369a4 commit 602738b

File tree

7 files changed

+212
-103
lines changed

7 files changed

+212
-103
lines changed

solution/0500-0599/0528.Random Pick with Weight/README.md

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,20 @@ solution.pickIndex(); // 返回 0,返回下标 0,返回该下标概率为 1/
7979
class Solution:
8080

8181
def __init__(self, w: List[int]):
82-
n = len(w)
83-
self.presum = [0] * (n + 1)
84-
for i in range(n):
85-
self.presum[i + 1] = self.presum[i] + w[i]
82+
self.s = [0]
83+
for c in w:
84+
self.s.append(self.s[-1] + c)
8685

8786
def pickIndex(self) -> int:
88-
n = len(self.presum)
89-
x = random.randint(1, self.presum[-1])
90-
left, right = 0, n - 2
87+
x = random.randint(1, self.s[-1])
88+
left, right = 1, len(self.s) - 1
9189
while left < right:
9290
mid = (left + right) >> 1
93-
if self.presum[mid + 1] >= x:
91+
if self.s[mid] >= x:
9492
right = mid
9593
else:
9694
left = mid + 1
97-
return left
95+
return left - 1
9896

9997
# Your Solution object will be instantiated and called as such:
10098
# obj = Solution(w)
@@ -107,29 +105,29 @@ class Solution:
107105

108106
```java
109107
class Solution {
110-
private int[] presum;
108+
private int[] s;
109+
private Random random = new Random();
111110

112111
public Solution(int[] w) {
113112
int n = w.length;
114-
presum = new int[n + 1];
113+
s = new int[n + 1];
115114
for (int i = 0; i < n; ++i) {
116-
presum[i + 1] = presum[i] + w[i];
115+
s[i + 1] = s[i] + w[i];
117116
}
118117
}
119118

120119
public int pickIndex() {
121-
int n = presum.length;
122-
int x = (int) (Math.random() * presum[n - 1]) + 1;
123-
int left = 0, right = n - 2;
120+
int x = 1 + random.nextInt(s[s.length - 1]);
121+
int left = 1, right = s.length - 1;
124122
while (left < right) {
125123
int mid = (left + right) >> 1;
126-
if (presum[mid + 1] >= x) {
124+
if (s[mid] >= x) {
127125
right = mid;
128126
} else {
129127
left = mid + 1;
130128
}
131129
}
132-
return left;
130+
return left - 1;
133131
}
134132
}
135133

@@ -145,25 +143,25 @@ class Solution {
145143
```cpp
146144
class Solution {
147145
public:
148-
vector<int> presum;
146+
vector<int> s;
149147

150148
Solution(vector<int>& w) {
151149
int n = w.size();
152-
presum.resize(n + 1);
153-
for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + w[i];
150+
s.resize(n + 1);
151+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + w[i];
154152
}
155153

156154
int pickIndex() {
157-
int n = presum.size();
158-
int x = rand() % presum[n - 1] + 1;
159-
int left = 0, right = n - 2;
155+
int n = s.size();
156+
int x = 1 + rand() % s[n - 1];
157+
int left = 1, right = n - 1;
160158
while (left < right)
161159
{
162160
int mid = left + right >> 1;
163-
if (presum[mid + 1] >= x) right = mid;
161+
if (s[mid] >= x) right = mid;
164162
else left = mid + 1;
165163
}
166-
return left;
164+
return left - 1;
167165
}
168166
};
169167

@@ -178,31 +176,31 @@ public:
178176

179177
```go
180178
type Solution struct {
181-
presum []int
179+
s []int
182180
}
183181

184182
func Constructor(w []int) Solution {
185183
n := len(w)
186-
pre := make([]int, n+1)
184+
s := make([]int, n+1)
187185
for i := 0; i < n; i++ {
188-
pre[i+1] = pre[i] + w[i]
186+
s[i+1] = s[i] + w[i]
189187
}
190-
return Solution{pre}
188+
return Solution{s}
191189
}
192190

193191
func (this *Solution) PickIndex() int {
194-
n := len(this.presum)
195-
x := rand.Intn(this.presum[n-1]) + 1
196-
left, right := 0, n-2
192+
n := len(this.s)
193+
x := 1 + rand.Intn(this.s[n-1])
194+
left, right := 1, n-1
197195
for left < right {
198196
mid := (left + right) >> 1
199-
if this.presum[mid+1] >= x {
197+
if this.s[mid] >= x {
200198
right = mid
201199
} else {
202200
left = mid + 1
203201
}
204202
}
205-
return left
203+
return left - 1
206204
}
207205

208206
/**
@@ -212,6 +210,46 @@ func (this *Solution) PickIndex() int {
212210
*/
213211
```
214212

213+
### **JavaScript**
214+
215+
```js
216+
/**
217+
* @param {number[]} w
218+
*/
219+
var Solution = function (w) {
220+
const n = w.length;
221+
this.s = new Array(n + 1).fill(0);
222+
for (let i = 0; i < n; ++i) {
223+
this.s[i + 1] = this.s[i] + w[i];
224+
}
225+
};
226+
227+
/**
228+
* @return {number}
229+
*/
230+
Solution.prototype.pickIndex = function () {
231+
const n = this.s.length;
232+
const x = 1 + Math.floor(Math.random() * this.s[n - 1]);
233+
let left = 1,
234+
right = n - 1;
235+
while (left < right) {
236+
const mid = (left + right) >> 1;
237+
if (this.s[mid] >= x) {
238+
right = mid;
239+
} else {
240+
left = mid + 1;
241+
}
242+
}
243+
return left - 1;
244+
};
245+
246+
/**
247+
* Your Solution object will be instantiated and called as such:
248+
* var obj = new Solution(w)
249+
* var param_1 = obj.pickIndex()
250+
*/
251+
```
252+
215253
### **...**
216254

217255
```

solution/0500-0599/0528.Random Pick with Weight/README_EN.md

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,20 @@ and so on.
7171
class Solution:
7272

7373
def __init__(self, w: List[int]):
74-
n = len(w)
75-
self.presum = [0] * (n + 1)
76-
for i in range(n):
77-
self.presum[i + 1] = self.presum[i] + w[i]
74+
self.s = [0]
75+
for c in w:
76+
self.s.append(self.s[-1] + c)
7877

7978
def pickIndex(self) -> int:
80-
n = len(self.presum)
81-
x = random.randint(1, self.presum[-1])
82-
left, right = 0, n - 2
79+
x = random.randint(1, self.s[-1])
80+
left, right = 1, len(self.s) - 1
8381
while left < right:
8482
mid = (left + right) >> 1
85-
if self.presum[mid + 1] >= x:
83+
if self.s[mid] >= x:
8684
right = mid
8785
else:
8886
left = mid + 1
89-
return left
87+
return left - 1
9088

9189
# Your Solution object will be instantiated and called as such:
9290
# obj = Solution(w)
@@ -97,29 +95,29 @@ class Solution:
9795

9896
```java
9997
class Solution {
100-
private int[] presum;
98+
private int[] s;
99+
private Random random = new Random();
101100

102101
public Solution(int[] w) {
103102
int n = w.length;
104-
presum = new int[n + 1];
103+
s = new int[n + 1];
105104
for (int i = 0; i < n; ++i) {
106-
presum[i + 1] = presum[i] + w[i];
105+
s[i + 1] = s[i] + w[i];
107106
}
108107
}
109108

110109
public int pickIndex() {
111-
int n = presum.length;
112-
int x = (int) (Math.random() * presum[n - 1]) + 1;
113-
int left = 0, right = n - 2;
110+
int x = 1 + random.nextInt(s[s.length - 1]);
111+
int left = 1, right = s.length - 1;
114112
while (left < right) {
115113
int mid = (left + right) >> 1;
116-
if (presum[mid + 1] >= x) {
114+
if (s[mid] >= x) {
117115
right = mid;
118116
} else {
119117
left = mid + 1;
120118
}
121119
}
122-
return left;
120+
return left - 1;
123121
}
124122
}
125123

@@ -135,25 +133,25 @@ class Solution {
135133
```cpp
136134
class Solution {
137135
public:
138-
vector<int> presum;
136+
vector<int> s;
139137

140138
Solution(vector<int>& w) {
141139
int n = w.size();
142-
presum.resize(n + 1);
143-
for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + w[i];
140+
s.resize(n + 1);
141+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + w[i];
144142
}
145143

146144
int pickIndex() {
147-
int n = presum.size();
148-
int x = rand() % presum[n - 1] + 1;
149-
int left = 0, right = n - 2;
145+
int n = s.size();
146+
int x = 1 + rand() % s[n - 1];
147+
int left = 1, right = n - 1;
150148
while (left < right)
151149
{
152150
int mid = left + right >> 1;
153-
if (presum[mid + 1] >= x) right = mid;
151+
if (s[mid] >= x) right = mid;
154152
else left = mid + 1;
155153
}
156-
return left;
154+
return left - 1;
157155
}
158156
};
159157

@@ -168,31 +166,31 @@ public:
168166

169167
```go
170168
type Solution struct {
171-
presum []int
169+
s []int
172170
}
173171

174172
func Constructor(w []int) Solution {
175173
n := len(w)
176-
pre := make([]int, n+1)
174+
s := make([]int, n+1)
177175
for i := 0; i < n; i++ {
178-
pre[i+1] = pre[i] + w[i]
176+
s[i+1] = s[i] + w[i]
179177
}
180-
return Solution{pre}
178+
return Solution{s}
181179
}
182180

183181
func (this *Solution) PickIndex() int {
184-
n := len(this.presum)
185-
x := rand.Intn(this.presum[n-1]) + 1
186-
left, right := 0, n-2
182+
n := len(this.s)
183+
x := 1 + rand.Intn(this.s[n-1])
184+
left, right := 1, n-1
187185
for left < right {
188186
mid := (left + right) >> 1
189-
if this.presum[mid+1] >= x {
187+
if this.s[mid] >= x {
190188
right = mid
191189
} else {
192190
left = mid + 1
193191
}
194192
}
195-
return left
193+
return left - 1
196194
}
197195

198196
/**
@@ -202,6 +200,46 @@ func (this *Solution) PickIndex() int {
202200
*/
203201
```
204202

203+
### **JavaScript**
204+
205+
```js
206+
/**
207+
* @param {number[]} w
208+
*/
209+
var Solution = function (w) {
210+
const n = w.length;
211+
this.s = new Array(n + 1).fill(0);
212+
for (let i = 0; i < n; ++i) {
213+
this.s[i + 1] = this.s[i] + w[i];
214+
}
215+
};
216+
217+
/**
218+
* @return {number}
219+
*/
220+
Solution.prototype.pickIndex = function () {
221+
const n = this.s.length;
222+
const x = 1 + Math.floor(Math.random() * this.s[n - 1]);
223+
let left = 1,
224+
right = n - 1;
225+
while (left < right) {
226+
const mid = (left + right) >> 1;
227+
if (this.s[mid] >= x) {
228+
right = mid;
229+
} else {
230+
left = mid + 1;
231+
}
232+
}
233+
return left - 1;
234+
};
235+
236+
/**
237+
* Your Solution object will be instantiated and called as such:
238+
* var obj = new Solution(w)
239+
* var param_1 = obj.pickIndex()
240+
*/
241+
```
242+
205243
### **...**
206244

207245
```

0 commit comments

Comments
 (0)