Skip to content

Commit a6ad785

Browse files
committed
feat: add solutions to lc problem: No.0710
No.0710.Random Pick with Blacklist
1 parent 4d38172 commit a6ad785

File tree

6 files changed

+389
-2
lines changed

6 files changed

+389
-2
lines changed

solution/0700-0799/0710.Random Pick with Blacklist/README.md

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,155 @@ solution.pick(); // 返回 4
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:哈希表**
60+
5961
<!-- tabs:start -->
6062

6163
### **Python3**
6264

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

6567
```python
66-
68+
class Solution:
69+
70+
def __init__(self, n: int, blacklist: List[int]):
71+
self.k = n - len(blacklist)
72+
self.d = {}
73+
i = self.k
74+
black = set(blacklist)
75+
for b in blacklist:
76+
if b < self.k:
77+
while i in black:
78+
i += 1
79+
self.d[b] = i
80+
i += 1
81+
82+
def pick(self) -> int:
83+
x = randrange(self.k)
84+
return self.d.get(x, x)
85+
86+
87+
# Your Solution object will be instantiated and called as such:
88+
# obj = Solution(n, blacklist)
89+
# param_1 = obj.pick()
6790
```
6891

6992
### **Java**
7093

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

7396
```java
97+
class Solution {
98+
private Map<Integer, Integer> d = new HashMap<>();
99+
private Random rand = new Random();
100+
private int k;
101+
102+
public Solution(int n, int[] blacklist) {
103+
k = n - blacklist.length;
104+
int i = k;
105+
Set<Integer> black = new HashSet<>();
106+
for (int b : blacklist) {
107+
black.add(b);
108+
}
109+
for (int b : blacklist) {
110+
if (b < k) {
111+
while (black.contains(i)) {
112+
++i;
113+
}
114+
d.put(b, i++);
115+
}
116+
}
117+
}
118+
119+
public int pick() {
120+
int x = rand.nextInt(k);
121+
return d.getOrDefault(x, x);
122+
}
123+
}
124+
125+
/**
126+
* Your Solution object will be instantiated and called as such:
127+
* Solution obj = new Solution(n, blacklist);
128+
* int param_1 = obj.pick();
129+
*/
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
unordered_map<int, int> d;
138+
int k;
139+
140+
Solution(int n, vector<int>& blacklist) {
141+
k = n - blacklist.size();
142+
int i = k;
143+
unordered_set<int> black(blacklist.begin(), blacklist.end());
144+
for (int& b : blacklist)
145+
{
146+
if (b < k)
147+
{
148+
while (black.count(i)) ++i;
149+
d[b] = i++;
150+
}
151+
}
152+
}
153+
154+
int pick() {
155+
int x = rand() % k;
156+
return d.count(x) ? d[x] : x;
157+
}
158+
};
159+
160+
/**
161+
* Your Solution object will be instantiated and called as such:
162+
* Solution* obj = new Solution(n, blacklist);
163+
* int param_1 = obj->pick();
164+
*/
165+
```
74166

167+
### **Go**
168+
169+
```go
170+
type Solution struct {
171+
d map[int]int
172+
k int
173+
}
174+
175+
func Constructor(n int, blacklist []int) Solution {
176+
k := n - len(blacklist)
177+
i := k
178+
black := map[int]bool{}
179+
for _, b := range blacklist {
180+
black[b] = true
181+
}
182+
d := map[int]int{}
183+
for _, b := range blacklist {
184+
if b < k {
185+
for black[i] {
186+
i++
187+
}
188+
d[b] = i
189+
i++
190+
}
191+
}
192+
return Solution{d, k}
193+
}
194+
195+
func (this *Solution) Pick() int {
196+
x := rand.Intn(this.k)
197+
if v, ok := this.d[x]; ok {
198+
return v
199+
}
200+
return x
201+
}
202+
203+
/**
204+
* Your Solution object will be instantiated and called as such:
205+
* obj := Constructor(n, blacklist);
206+
* param_1 := obj.Pick();
207+
*/
75208
```
76209

77210
### **...**

solution/0700-0799/0710.Random Pick with Blacklist/README_EN.md

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,144 @@ solution.pick(); // return 4
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
60+
def __init__(self, n: int, blacklist: List[int]):
61+
self.k = n - len(blacklist)
62+
self.d = {}
63+
i = self.k
64+
black = set(blacklist)
65+
for b in blacklist:
66+
if b < self.k:
67+
while i in black:
68+
i += 1
69+
self.d[b] = i
70+
i += 1
71+
72+
def pick(self) -> int:
73+
x = randrange(self.k)
74+
return self.d.get(x, x)
75+
76+
77+
# Your Solution object will be instantiated and called as such:
78+
# obj = Solution(n, blacklist)
79+
# param_1 = obj.pick()
5980
```
6081

6182
### **Java**
6283

6384
```java
85+
class Solution {
86+
private Map<Integer, Integer> d = new HashMap<>();
87+
private Random rand = new Random();
88+
private int k;
89+
90+
public Solution(int n, int[] blacklist) {
91+
k = n - blacklist.length;
92+
int i = k;
93+
Set<Integer> black = new HashSet<>();
94+
for (int b : blacklist) {
95+
black.add(b);
96+
}
97+
for (int b : blacklist) {
98+
if (b < k) {
99+
while (black.contains(i)) {
100+
++i;
101+
}
102+
d.put(b, i++);
103+
}
104+
}
105+
}
106+
107+
public int pick() {
108+
int x = rand.nextInt(k);
109+
return d.getOrDefault(x, x);
110+
}
111+
}
112+
113+
/**
114+
* Your Solution object will be instantiated and called as such:
115+
* Solution obj = new Solution(n, blacklist);
116+
* int param_1 = obj.pick();
117+
*/
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
unordered_map<int, int> d;
126+
int k;
127+
128+
Solution(int n, vector<int>& blacklist) {
129+
k = n - blacklist.size();
130+
int i = k;
131+
unordered_set<int> black(blacklist.begin(), blacklist.end());
132+
for (int& b : blacklist)
133+
{
134+
if (b < k)
135+
{
136+
while (black.count(i)) ++i;
137+
d[b] = i++;
138+
}
139+
}
140+
}
141+
142+
int pick() {
143+
int x = rand() % k;
144+
return d.count(x) ? d[x] : x;
145+
}
146+
};
147+
148+
/**
149+
* Your Solution object will be instantiated and called as such:
150+
* Solution* obj = new Solution(n, blacklist);
151+
* int param_1 = obj->pick();
152+
*/
153+
```
64154

155+
### **Go**
156+
157+
```go
158+
type Solution struct {
159+
d map[int]int
160+
k int
161+
}
162+
163+
func Constructor(n int, blacklist []int) Solution {
164+
k := n - len(blacklist)
165+
i := k
166+
black := map[int]bool{}
167+
for _, b := range blacklist {
168+
black[b] = true
169+
}
170+
d := map[int]int{}
171+
for _, b := range blacklist {
172+
if b < k {
173+
for black[i] {
174+
i++
175+
}
176+
d[b] = i
177+
i++
178+
}
179+
}
180+
return Solution{d, k}
181+
}
182+
183+
func (this *Solution) Pick() int {
184+
x := rand.Intn(this.k)
185+
if v, ok := this.d[x]; ok {
186+
return v
187+
}
188+
return x
189+
}
190+
191+
/**
192+
* Your Solution object will be instantiated and called as such:
193+
* obj := Constructor(n, blacklist);
194+
* param_1 := obj.Pick();
195+
*/
65196
```
66197

67198
### **...**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
unordered_map<int, int> d;
4+
int k;
5+
6+
Solution(int n, vector<int>& blacklist) {
7+
k = n - blacklist.size();
8+
int i = k;
9+
unordered_set<int> black(blacklist.begin(), blacklist.end());
10+
for (int& b : blacklist)
11+
{
12+
if (b < k)
13+
{
14+
while (black.count(i)) ++i;
15+
d[b] = i++;
16+
}
17+
}
18+
}
19+
20+
int pick() {
21+
int x = rand() % k;
22+
return d.count(x) ? d[x] : x;
23+
}
24+
};
25+
26+
/**
27+
* Your Solution object will be instantiated and called as such:
28+
* Solution* obj = new Solution(n, blacklist);
29+
* int param_1 = obj->pick();
30+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
type Solution struct {
2+
d map[int]int
3+
k int
4+
}
5+
6+
func Constructor(n int, blacklist []int) Solution {
7+
k := n - len(blacklist)
8+
i := k
9+
black := map[int]bool{}
10+
for _, b := range blacklist {
11+
black[b] = true
12+
}
13+
d := map[int]int{}
14+
for _, b := range blacklist {
15+
if b < k {
16+
for black[i] {
17+
i++
18+
}
19+
d[b] = i
20+
i++
21+
}
22+
}
23+
return Solution{d, k}
24+
}
25+
26+
func (this *Solution) Pick() int {
27+
x := rand.Intn(this.k)
28+
if v, ok := this.d[x]; ok {
29+
return v
30+
}
31+
return x
32+
}
33+
34+
/**
35+
* Your Solution object will be instantiated and called as such:
36+
* obj := Constructor(n, blacklist);
37+
* param_1 := obj.Pick();
38+
*/

0 commit comments

Comments
 (0)