Skip to content

Commit 9538a31

Browse files
committed
feat: add solutions to lc problem: No.2397
No.2397.Maximum Rows Covered by Columns
1 parent 1f5f29d commit 9538a31

File tree

6 files changed

+161
-212
lines changed

6 files changed

+161
-212
lines changed

solution/2300-2399/2397.Maximum Rows Covered by Columns/README.md

Lines changed: 57 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55-
**方法一:DFS 或二进制枚举**
55+
**方法一:二进制枚举**
5656

57-
直接二进制枚举选中的列,然后判断是否覆盖所有行中的 `1`,若是,更新答案
57+
我们先将矩阵中的每一行转换成一个二进制数,记录在数组 $rows$ 中,其中 $rows[i]$ 表示第 $i$ 行对应的二进制数,而 $rows[i]$ 的第 $j$ 位表示第 $i$ 行第 $j$ 列的值
5858

59-
时间复杂度 $O(2^n\times n)$,空间复杂度 $O(m)$,其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
59+
接下来,我们枚举所有的 $2^n$ 种列选择方案,其中 $n$ 为矩阵的列数。对于每一种列选择方案,我们判断是否选中了 $numSelect$ 列,如果不是,则跳过。否则,我们统计矩阵中有多少行中的所有 $1$ 都被选中的列覆盖,即统计有多少行的二进制数 $rows[i]$ 与列选择方案 $mask$ 按位与的结果等于 $rows[i]$,并更新最大的行数。
60+
61+
时间复杂度 $O(2^n \times m)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
6062

6163
<!-- tabs:start -->
6264

@@ -66,43 +68,17 @@
6668

6769
```python
6870
class Solution:
69-
def maximumRows(self, mat: List[List[int]], cols: int) -> int:
70-
def dfs(mask, i):
71-
if i > n or mask.bit_count() > cols:
72-
return
73-
nonlocal ans
74-
if i == n:
75-
t = sum((v & mask) == v for v in arr)
76-
ans = max(ans, t)
77-
return
78-
dfs(mask, i + 1)
79-
dfs(mask | 1 << i, i + 1)
80-
81-
arr = []
82-
ans, n = 0, len(mat[0])
83-
for i, row in enumerate(mat):
84-
x = 0
85-
for j, v in enumerate(row):
86-
x |= v << j
87-
arr.append(x)
88-
dfs(0, 0)
89-
return ans
90-
```
91-
92-
```python
93-
class Solution:
94-
def maximumRows(self, mat: List[List[int]], cols: int) -> int:
95-
arr = []
96-
for i, row in enumerate(mat):
97-
x = 0
98-
for j, v in enumerate(row):
99-
x |= v << j
100-
arr.append(x)
101-
ans, n = 0, len(mat[0])
102-
for mask in range(1, 1 << n | 1):
103-
if mask.bit_count() > cols:
71+
def maximumRows(self, matrix: List[List[int]], numSelect: int) -> int:
72+
rows = []
73+
for row in matrix:
74+
mask = reduce(or_, (1 << j for j, x in enumerate(row) if x), 0)
75+
rows.append(mask)
76+
77+
ans = 0
78+
for mask in range(1 << len(matrix[0])):
79+
if mask.bit_count() != numSelect:
10480
continue
105-
t = sum((v & mask) == v for v in arr)
81+
t = sum((x & mask) == x for x in rows)
10682
ans = max(ans, t)
10783
return ans
10884
```
@@ -113,25 +89,24 @@ class Solution:
11389

11490
```java
11591
class Solution {
116-
private int ans;
117-
public int maximumRows(int[][] mat, int cols) {
118-
int m = mat.length, n = mat[0].length;
119-
int[] arr = new int[m];
92+
public int maximumRows(int[][] matrix, int numSelect) {
93+
int m = matrix.length, n = matrix[0].length;
94+
int[] rows = new int[m];
12095
for (int i = 0; i < m; ++i) {
121-
int x = 0;
12296
for (int j = 0; j < n; ++j) {
123-
x |= mat[i][j] << j;
97+
if (matrix[i][j] == 1) {
98+
rows[i] |= 1 << j;
99+
}
124100
}
125-
arr[i] = x;
126101
}
127102
int ans = 0;
128-
for (int mask = 1; mask <= 1 << n; ++mask) {
129-
if (Integer.bitCount(mask) > cols) {
103+
for (int mask = 1; mask < 1 << n; ++mask) {
104+
if (Integer.bitCount(mask) != numSelect) {
130105
continue;
131106
}
132107
int t = 0;
133-
for (int v : arr) {
134-
if ((v & mask) == v) {
108+
for (int x : rows) {
109+
if ((x & mask) == x) {
135110
++t;
136111
}
137112
}
@@ -147,19 +122,26 @@ class Solution {
147122
```cpp
148123
class Solution {
149124
public:
150-
int maximumRows(vector<vector<int>>& mat, int cols) {
151-
int m = mat.size(), n = mat[0].size();
152-
vector<int> arr(m);
125+
int maximumRows(vector<vector<int>>& matrix, int numSelect) {
126+
int m = matrix.size(), n = matrix[0].size();
127+
int rows[m];
128+
memset(rows, 0, sizeof(rows));
153129
for (int i = 0; i < m; ++i) {
154-
int x = 0;
155-
for (int j = 0; j < n; ++j) x |= mat[i][j] << j;
156-
arr[i] = x;
130+
for (int j = 0; j < n; ++j) {
131+
if (matrix[i][j]) {
132+
rows[i] |= 1 << j;
133+
}
134+
}
157135
}
158136
int ans = 0;
159-
for (int mask = 1; mask <= 1 << n; ++mask) {
160-
if (__builtin_popcount(mask) > cols) continue;
137+
for (int mask = 1; mask < 1 << n; ++mask) {
138+
if (__builtin_popcount(mask) != numSelect) {
139+
continue;
140+
}
161141
int t = 0;
162-
for (int v : arr) t += (v & mask) == v;
142+
for (int x : rows) {
143+
t += (x & mask) == x;
144+
}
163145
ans = max(ans, t);
164146
}
165147
return ans;
@@ -170,37 +152,31 @@ public:
170152
### **Go**
171153
172154
```go
173-
func maximumRows(mat [][]int, cols int) int {
174-
m, n := len(mat), len(mat[0])
175-
arr := make([]int, m)
176-
for i, row := range mat {
177-
x := 0
178-
for j, v := range row {
179-
x |= v << j
155+
func maximumRows(matrix [][]int, numSelect int) (ans int) {
156+
m, n := len(matrix), len(matrix[0])
157+
rows := make([]int, m)
158+
for i, row := range matrix {
159+
for j, x := range row {
160+
if x == 1 {
161+
rows[i] |= 1 << j
162+
}
180163
}
181-
arr[i] = x
182164
}
183-
ans := 0
184-
for mask := 1; mask <= 1<<n; mask++ {
185-
if bits.OnesCount(uint(mask)) != cols {
165+
for mask := 1; mask < 1<<n; mask++ {
166+
if bits.OnesCount(uint(mask)) != numSelect {
186167
continue
187168
}
188169
t := 0
189-
for _, v := range arr {
190-
if (v & mask) == v {
170+
for _, x := range rows {
171+
if (x & mask) == x {
191172
t++
192173
}
193174
}
194-
ans = max(ans, t)
195-
}
196-
return ans
197-
}
198-
199-
func max(a, b int) int {
200-
if a > b {
201-
return a
175+
if ans < t {
176+
ans = t
177+
}
202178
}
203-
return b
179+
return
204180
}
205181
```
206182

solution/2300-2399/2397.Maximum Rows Covered by Columns/README_EN.md

Lines changed: 52 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -61,43 +61,17 @@ Therefore, we return 2.
6161

6262
```python
6363
class Solution:
64-
def maximumRows(self, mat: List[List[int]], cols: int) -> int:
65-
def dfs(mask, i):
66-
if i > n or mask.bit_count() > cols:
67-
return
68-
nonlocal ans
69-
if i == n:
70-
t = sum((v & mask) == v for v in arr)
71-
ans = max(ans, t)
72-
return
73-
dfs(mask, i + 1)
74-
dfs(mask | 1 << i, i + 1)
75-
76-
arr = []
77-
ans, n = 0, len(mat[0])
78-
for i, row in enumerate(mat):
79-
x = 0
80-
for j, v in enumerate(row):
81-
x |= v << j
82-
arr.append(x)
83-
dfs(0, 0)
84-
return ans
85-
```
86-
87-
```python
88-
class Solution:
89-
def maximumRows(self, mat: List[List[int]], cols: int) -> int:
90-
arr = []
91-
for i, row in enumerate(mat):
92-
x = 0
93-
for j, v in enumerate(row):
94-
x |= v << j
95-
arr.append(x)
96-
ans, n = 0, len(mat[0])
97-
for mask in range(1, 1 << n | 1):
98-
if mask.bit_count() > cols:
64+
def maximumRows(self, matrix: List[List[int]], numSelect: int) -> int:
65+
rows = []
66+
for row in matrix:
67+
mask = reduce(or_, (1 << j for j, x in enumerate(row) if x), 0)
68+
rows.append(mask)
69+
70+
ans = 0
71+
for mask in range(1 << len(matrix[0])):
72+
if mask.bit_count() != numSelect:
9973
continue
100-
t = sum((v & mask) == v for v in arr)
74+
t = sum((x & mask) == x for x in rows)
10175
ans = max(ans, t)
10276
return ans
10377
```
@@ -106,25 +80,24 @@ class Solution:
10680

10781
```java
10882
class Solution {
109-
private int ans;
110-
public int maximumRows(int[][] mat, int cols) {
111-
int m = mat.length, n = mat[0].length;
112-
int[] arr = new int[m];
83+
public int maximumRows(int[][] matrix, int numSelect) {
84+
int m = matrix.length, n = matrix[0].length;
85+
int[] rows = new int[m];
11386
for (int i = 0; i < m; ++i) {
114-
int x = 0;
11587
for (int j = 0; j < n; ++j) {
116-
x |= mat[i][j] << j;
88+
if (matrix[i][j] == 1) {
89+
rows[i] |= 1 << j;
90+
}
11791
}
118-
arr[i] = x;
11992
}
12093
int ans = 0;
121-
for (int mask = 1; mask <= 1 << n; ++mask) {
122-
if (Integer.bitCount(mask) > cols) {
94+
for (int mask = 1; mask < 1 << n; ++mask) {
95+
if (Integer.bitCount(mask) != numSelect) {
12396
continue;
12497
}
12598
int t = 0;
126-
for (int v : arr) {
127-
if ((v & mask) == v) {
99+
for (int x : rows) {
100+
if ((x & mask) == x) {
128101
++t;
129102
}
130103
}
@@ -140,19 +113,26 @@ class Solution {
140113
```cpp
141114
class Solution {
142115
public:
143-
int maximumRows(vector<vector<int>>& mat, int cols) {
144-
int m = mat.size(), n = mat[0].size();
145-
vector<int> arr(m);
116+
int maximumRows(vector<vector<int>>& matrix, int numSelect) {
117+
int m = matrix.size(), n = matrix[0].size();
118+
int rows[m];
119+
memset(rows, 0, sizeof(rows));
146120
for (int i = 0; i < m; ++i) {
147-
int x = 0;
148-
for (int j = 0; j < n; ++j) x |= mat[i][j] << j;
149-
arr[i] = x;
121+
for (int j = 0; j < n; ++j) {
122+
if (matrix[i][j]) {
123+
rows[i] |= 1 << j;
124+
}
125+
}
150126
}
151127
int ans = 0;
152-
for (int mask = 1; mask <= 1 << n; ++mask) {
153-
if (__builtin_popcount(mask) > cols) continue;
128+
for (int mask = 1; mask < 1 << n; ++mask) {
129+
if (__builtin_popcount(mask) != numSelect) {
130+
continue;
131+
}
154132
int t = 0;
155-
for (int v : arr) t += (v & mask) == v;
133+
for (int x : rows) {
134+
t += (x & mask) == x;
135+
}
156136
ans = max(ans, t);
157137
}
158138
return ans;
@@ -163,37 +143,31 @@ public:
163143
### **Go**
164144
165145
```go
166-
func maximumRows(mat [][]int, cols int) int {
167-
m, n := len(mat), len(mat[0])
168-
arr := make([]int, m)
169-
for i, row := range mat {
170-
x := 0
171-
for j, v := range row {
172-
x |= v << j
146+
func maximumRows(matrix [][]int, numSelect int) (ans int) {
147+
m, n := len(matrix), len(matrix[0])
148+
rows := make([]int, m)
149+
for i, row := range matrix {
150+
for j, x := range row {
151+
if x == 1 {
152+
rows[i] |= 1 << j
153+
}
173154
}
174-
arr[i] = x
175155
}
176-
ans := 0
177-
for mask := 1; mask <= 1<<n; mask++ {
178-
if bits.OnesCount(uint(mask)) != cols {
156+
for mask := 1; mask < 1<<n; mask++ {
157+
if bits.OnesCount(uint(mask)) != numSelect {
179158
continue
180159
}
181160
t := 0
182-
for _, v := range arr {
183-
if (v & mask) == v {
161+
for _, x := range rows {
162+
if (x & mask) == x {
184163
t++
185164
}
186165
}
187-
ans = max(ans, t)
188-
}
189-
return ans
190-
}
191-
192-
func max(a, b int) int {
193-
if a > b {
194-
return a
166+
if ans < t {
167+
ans = t
168+
}
195169
}
196-
return b
170+
return
197171
}
198172
```
199173

0 commit comments

Comments
 (0)