Skip to content

Commit 25672ad

Browse files
authored
Merge branch 'main' into main
2 parents 47efe15 + fb7fbb0 commit 25672ad

File tree

5 files changed

+96
-6
lines changed

5 files changed

+96
-6
lines changed

solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,29 @@ function removeOnes(grid: number[][]): boolean {
178178
}
179179
```
180180

181+
#### Rust
182+
183+
```rust
184+
use std::collections::HashSet;
185+
186+
impl Solution {
187+
pub fn remove_ones(grid: Vec<Vec<i32>>) -> bool {
188+
let n = grid[0].len();
189+
let mut set = HashSet::new();
190+
191+
for row in grid.iter() {
192+
let mut pattern = String::with_capacity(n);
193+
for &x in row.iter() {
194+
pattern.push(((row[0] ^ x) as u8 + b'0') as char);
195+
}
196+
set.insert(pattern);
197+
}
198+
199+
set.len() == 1
200+
}
201+
}
202+
```
203+
181204
<!-- tabs:end -->
182205

183206
<!-- solution:end -->

solution/2100-2199/2128.Remove All Ones With Row and Column Flips/README_EN.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,27 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Hash Table
72+
73+
We observe that if two rows in the matrix satisfy one of the following conditions, they can be made equal by flipping certain columns:
74+
75+
1. The corresponding elements of the two rows are equal, i.e., if one row is $1,0,0,1$, the other row is also $1,0,0,1$;
76+
1. The corresponding elements of the two rows are opposite, i.e., if one row is $1,0,0,1$, the other row is $0,1,1,0$.
77+
78+
We call two rows that satisfy one of the above conditions "equivalent rows." The answer to the problem is the maximum number of equivalent rows in the matrix.
79+
80+
Therefore, we can traverse each row of the matrix and convert each row into an "equivalent row" that starts with $0$. Specifically:
81+
82+
- If the first element of the current row is $0$, keep the row unchanged;
83+
- If the first element of the current row is $1$, flip every element in the row, i.e., $0$ becomes $1$, $1$ becomes $0$. In other words, we flip rows starting with $1$ into "equivalent rows" starting with $0$.
84+
85+
In this way, we only need to use a hash table to count each converted row. If the hash table contains only one element at the end, it means we can remove all $1$s from the matrix by flipping rows or columns.
86+
87+
The time complexity is $O(mn)$ and the space complexity is $O(m)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively.
88+
89+
Related problem:
90+
91+
- [1072. Flip Columns For Maximum Number of Equal Rows](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1072.Flip%20Columns%20For%20Maximum%20Number%20of%20Equal%20Rows/README_EN.md)
7292

7393
<!-- tabs:start -->
7494

@@ -159,6 +179,29 @@ function removeOnes(grid: number[][]): boolean {
159179
}
160180
```
161181

182+
#### Rust
183+
184+
```rust
185+
use std::collections::HashSet;
186+
187+
impl Solution {
188+
pub fn remove_ones(grid: Vec<Vec<i32>>) -> bool {
189+
let n = grid[0].len();
190+
let mut set = HashSet::new();
191+
192+
for row in grid.iter() {
193+
let mut pattern = String::with_capacity(n);
194+
for &x in row.iter() {
195+
pattern.push(((row[0] ^ x) as u8 + b'0') as char);
196+
}
197+
set.insert(pattern);
198+
}
199+
200+
set.len() == 1
201+
}
202+
}
203+
```
204+
162205
<!-- tabs:end -->
163206

164207
<!-- solution:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::collections::HashSet;
2+
3+
impl Solution {
4+
pub fn remove_ones(grid: Vec<Vec<i32>>) -> bool {
5+
let n = grid[0].len();
6+
let mut set = HashSet::new();
7+
8+
for row in grid.iter() {
9+
let mut pattern = String::with_capacity(n);
10+
for &x in row.iter() {
11+
pattern.push(((row[0] ^ x) as u8 + b'0') as char);
12+
}
13+
set.insert(pattern);
14+
}
15+
16+
set.len() == 1
17+
}
18+
}

solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,11 @@ tags:
8282

8383
<!-- solution:start -->
8484

85-
### 方法一:链表转成列表(数组)求解
85+
### 方法一:模拟
8686

87-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
87+
我们可以将链表中的节点值依次存入数组中,然后使用双指针分别指向数组的开头和结尾,计算每对孪生节点的孪生和,取最大值即为答案。
88+
89+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是链表的节点数。
8890

8991
<!-- tabs:start -->
9092

solution/2100-2199/2130.Maximum Twin Sum of a Linked List/README_EN.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tags:
3939
<strong>Explanation:</strong>
4040
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
4141
There are no other nodes with twins in the linked list.
42-
Thus, the maximum twin sum of the linked list is 6.
42+
Thus, the maximum twin sum of the linked list is 6.
4343
</pre>
4444

4545
<p><strong class="example">Example 2:</strong></p>
@@ -51,7 +51,7 @@ Thus, the maximum twin sum of the linked list is 6.
5151
The nodes with twins present in this linked list are:
5252
- Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.
5353
- Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
54-
Thus, the maximum twin sum of the linked list is max(7, 4) = 7.
54+
Thus, the maximum twin sum of the linked list is max(7, 4) = 7.
5555
</pre>
5656

5757
<p><strong class="example">Example 3:</strong></p>
@@ -77,7 +77,11 @@ There is only one node with a twin in the linked list having twin sum of 1 + 100
7777

7878
<!-- solution:start -->
7979

80-
### Solution 1
80+
### Solution 1: Simulation
81+
82+
We can store the values of the nodes in the linked list into an array, then use two pointers pointing to the beginning and end of the array to calculate the twin sum for each pair of twin nodes. The maximum twin sum is the answer.
83+
84+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes in the linked list.
8185

8286
<!-- tabs:start -->
8387

0 commit comments

Comments
 (0)