Skip to content

Commit 25a4447

Browse files
committed
feat: add solutions to lc problem: No.0519
No.0519.Random Flip Matrix
1 parent d85b7a1 commit 25a4447

File tree

4 files changed

+165
-23
lines changed

4 files changed

+165
-23
lines changed

solution/0500-0599/0519.Random Flip Matrix/README.md

+55-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
<p>输入包含两个列表:被调用的子程序和他们的参数。<code>Solution</code> 的构造函数有两个参数,分别为 <code>n_rows</code> 和 <code>n_cols</code>。<code>flip</code>&nbsp;和 <code>reset</code> 没有参数,参数总会以列表形式给出,哪怕该列表为空</p>
3838

39-
4039
## 解法
4140

4241
<!-- 这里可写通用的实现逻辑 -->
@@ -48,15 +47,68 @@
4847
<!-- 这里可写当前语言的特殊实现逻辑 -->
4948

5049
```python
51-
50+
class Solution:
51+
52+
def __init__(self, m: int, n: int):
53+
self.m = m
54+
self.n = n
55+
self.total = m * n
56+
self.mp = {}
57+
58+
def flip(self) -> List[int]:
59+
self.total -= 1
60+
x = random.randint(0, self.total)
61+
idx = self.mp.get(x, x)
62+
self.mp[x] = self.mp.get(self.total, self.total)
63+
return [idx // self.n, idx % self.n]
64+
65+
def reset(self) -> None:
66+
self.total = self.m * self.n
67+
self.mp.clear()
68+
69+
# Your Solution object will be instantiated and called as such:
70+
# obj = Solution(m, n)
71+
# param_1 = obj.flip()
72+
# obj.reset()
5273
```
5374

5475
### **Java**
5576

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

5879
```java
59-
80+
class Solution {
81+
private int m;
82+
private int n;
83+
private int total;
84+
private Random rand = new Random();
85+
private Map<Integer, Integer> mp = new HashMap<>();
86+
87+
public Solution(int m, int n) {
88+
this.m = m;
89+
this.n = n;
90+
this.total = m * n;
91+
}
92+
93+
public int[] flip() {
94+
int x = rand.nextInt(total--);
95+
int idx = mp.getOrDefault(x, x);
96+
mp.put(x, mp.getOrDefault(total, total));
97+
return new int[]{idx / n, idx % n};
98+
}
99+
100+
public void reset() {
101+
total = m * n;
102+
mp.clear();
103+
}
104+
}
105+
106+
/**
107+
* Your Solution object will be instantiated and called as such:
108+
* Solution obj = new Solution(m, n);
109+
* int[] param_1 = obj.flip();
110+
* obj.reset();
111+
*/
60112
```
61113

62114
### **...**

solution/0500-0599/0519.Random Flip Matrix/README_EN.md

+55-20
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,17 @@
66

77
<p>You are given the number of rows <code>n_rows</code>&nbsp;and number of columns <code>n_cols</code>&nbsp;of a&nbsp;2D&nbsp;binary matrix&nbsp;where all values are initially 0.&nbsp;Write a function <code>flip</code>&nbsp;which chooses&nbsp;a 0 value&nbsp;<a href="https://en.wikipedia.org/wiki/Discrete_uniform_distribution" target="_blank">uniformly at random</a>,&nbsp;changes it to 1,&nbsp;and then returns the position <code>[row.id, col.id]</code> of that value. Also, write a function <code>reset</code> which sets all values back to 0.&nbsp;<strong>Try to minimize the number of calls to system&#39;s Math.random()</strong> and optimize the time and&nbsp;space complexity.</p>
88

9-
10-
119
<p>Note:</p>
1210

13-
14-
1511
<ol>
1612
<li><code>1 &lt;= n_rows, n_cols&nbsp;&lt;= 10000</code></li>
1713
<li><code>0 &lt;= row.id &lt; n_rows</code> and <code>0 &lt;= col.id &lt; n_cols</code></li>
1814
<li><code>flip</code>&nbsp;will not be called when the matrix has no&nbsp;0 values left.</li>
1915
<li>the total number of calls to&nbsp;<code>flip</code>&nbsp;and <code>reset</code>&nbsp;will not exceed&nbsp;1000.</li>
2016
</ol>
2117

22-
23-
2418
<p><strong>Example 1:</strong></p>
2519

26-
27-
2820
<pre>
2921

3022
<strong>Input:
@@ -37,14 +29,10 @@
3729

3830
</pre>
3931

40-
41-
4232
<div>
4333

4434
<p><strong>Example 2:</strong></p>
4535

46-
47-
4836
<pre>
4937

5038
<strong>Input:
@@ -57,30 +45,77 @@
5745

5846
</div>
5947

60-
61-
6248
<p><strong>Explanation of Input Syntax:</strong></p>
6349

64-
65-
6650
<p>The input is two lists:&nbsp;the subroutines called&nbsp;and their&nbsp;arguments. <code>Solution</code>&#39;s constructor&nbsp;has two arguments, <code>n_rows</code> and <code>n_cols</code>.&nbsp;<code>flip</code>&nbsp;and <code>reset</code> have&nbsp;no&nbsp;arguments.&nbsp;Arguments&nbsp;are&nbsp;always wrapped with a list, even if there aren&#39;t any.</p>
6751

68-
69-
7052
## Solutions
7153

7254
<!-- tabs:start -->
7355

7456
### **Python3**
7557

7658
```python
77-
59+
class Solution:
60+
61+
def __init__(self, m: int, n: int):
62+
self.m = m
63+
self.n = n
64+
self.total = m * n
65+
self.mp = {}
66+
67+
def flip(self) -> List[int]:
68+
self.total -= 1
69+
x = random.randint(0, self.total)
70+
idx = self.mp.get(x, x)
71+
self.mp[x] = self.mp.get(self.total, self.total)
72+
return [idx // self.n, idx % self.n]
73+
74+
def reset(self) -> None:
75+
self.total = self.m * self.n
76+
self.mp.clear()
77+
78+
# Your Solution object will be instantiated and called as such:
79+
# obj = Solution(m, n)
80+
# param_1 = obj.flip()
81+
# obj.reset()
7882
```
7983

8084
### **Java**
8185

8286
```java
83-
87+
class Solution {
88+
private int m;
89+
private int n;
90+
private int total;
91+
private Random rand = new Random();
92+
private Map<Integer, Integer> mp = new HashMap<>();
93+
94+
public Solution(int m, int n) {
95+
this.m = m;
96+
this.n = n;
97+
this.total = m * n;
98+
}
99+
100+
public int[] flip() {
101+
int x = rand.nextInt(total--);
102+
int idx = mp.getOrDefault(x, x);
103+
mp.put(x, mp.getOrDefault(total, total));
104+
return new int[]{idx / n, idx % n};
105+
}
106+
107+
public void reset() {
108+
total = m * n;
109+
mp.clear();
110+
}
111+
}
112+
113+
/**
114+
* Your Solution object will be instantiated and called as such:
115+
* Solution obj = new Solution(m, n);
116+
* int[] param_1 = obj.flip();
117+
* obj.reset();
118+
*/
84119
```
85120

86121
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
private int m;
3+
private int n;
4+
private int total;
5+
private Random rand = new Random();
6+
private Map<Integer, Integer> mp = new HashMap<>();
7+
8+
public Solution(int m, int n) {
9+
this.m = m;
10+
this.n = n;
11+
this.total = m * n;
12+
}
13+
14+
public int[] flip() {
15+
int x = rand.nextInt(total--);
16+
int idx = mp.getOrDefault(x, x);
17+
mp.put(x, mp.getOrDefault(total, total));
18+
return new int[]{idx / n, idx % n};
19+
}
20+
21+
public void reset() {
22+
total = m * n;
23+
mp.clear();
24+
}
25+
}
26+
27+
/**
28+
* Your Solution object will be instantiated and called as such:
29+
* Solution obj = new Solution(m, n);
30+
* int[] param_1 = obj.flip();
31+
* obj.reset();
32+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
3+
def __init__(self, m: int, n: int):
4+
self.m = m
5+
self.n = n
6+
self.total = m * n
7+
self.mp = {}
8+
9+
def flip(self) -> List[int]:
10+
self.total -= 1
11+
x = random.randint(0, self.total)
12+
idx = self.mp.get(x, x)
13+
self.mp[x] = self.mp.get(self.total, self.total)
14+
return [idx // self.n, idx % self.n]
15+
16+
def reset(self) -> None:
17+
self.total = self.m * self.n
18+
self.mp.clear()
19+
20+
# Your Solution object will be instantiated and called as such:
21+
# obj = Solution(m, n)
22+
# param_1 = obj.flip()
23+
# obj.reset()

0 commit comments

Comments
 (0)