Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7842612

Browse files
feat: add solutions to lc problem: No.2643 (doocs#4279)
No.2643.Row With Maximum Ones
1 parent 2800255 commit 7842612

File tree

9 files changed

+102
-75
lines changed

9 files changed

+102
-75
lines changed

‎solution/2600-2699/2643.Row With Maximum Ones/README.md‎

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tags:
3232
<pre>
3333
<strong>输入:</strong>mat = [[0,1],[1,0]]
3434
<strong>输出:</strong>[0,1]
35-
<strong>解释:</strong>两行中 1 的数量相同。所以返回下标最小的行,下标为 0 。该行 1 的数量为 1 。所以,答案为 [0,1] 。
35+
<strong>解释:</strong>两行中 1 的数量相同。所以返回下标最小的行,下标为 0 。该行 1 的数量为 1 。所以,答案为 [0,1] 。
3636
</pre>
3737

3838
<p><strong>示例 2:</strong></p>
@@ -69,9 +69,16 @@ tags:
6969

7070
### 方法一:模拟
7171

72-
我们直接遍历矩阵,统计每一行中 1ドル$ 的个数,更新最大值和对应的行下标。注意,如果当前行的 1ドル$ 的个数与最大值相等,我们需要选择行下标较小的那一行
72+
我们初始化一个数组 $\textit{ans} = [0, 0],ドル用于记录最多 1ドル$ 的行的下标和 1ドル$ 的数量
7373

74-
时间复杂度 $(m \times n),ドル其中 $m$ 和 $n$ 分别为矩阵的行数和列数。空间复杂度 $O(1)$。
74+
然后遍历矩阵的每一行,对于每一行:
75+
76+
- 计算该行 1ドル$ 的数量 $\textit{cnt}$(由于矩阵中只包含 0ドル$ 和 1ドル,ドル我们可以直接对该行求和);
77+
- 如果 $\textit{ans}[1] < \textit{cnt},ドル则更新 $\textit{ans} = [i, \textit{cnt}]$。
78+
79+
遍历结束后,返回 $\textit{ans}$。
80+
81+
时间复杂度 $O(m \times n),ドル其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。
7582

7683
<!-- tabs:start -->
7784

@@ -82,7 +89,7 @@ class Solution:
8289
def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
8390
ans = [0, 0]
8491
for i, row in enumerate(mat):
85-
cnt = row.count(1)
92+
cnt = sum(row)
8693
if ans[1] < cnt:
8794
ans = [i, cnt]
8895
return ans
@@ -97,9 +104,7 @@ class Solution {
97104
for (int i = 0; i < mat.length; ++i) {
98105
int cnt = 0;
99106
for (int x : mat[i]) {
100-
if (x == 1) {
101-
++cnt;
102-
}
107+
cnt += x;
103108
}
104109
if (ans[1] < cnt) {
105110
ans[0] = i;
@@ -119,13 +124,9 @@ public:
119124
vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
120125
vector<int> ans(2);
121126
for (int i = 0; i < mat.size(); ++i) {
122-
int cnt = 0;
123-
for (auto& x : mat[i]) {
124-
cnt += x == 1;
125-
}
127+
int cnt = accumulate(mat[i].begin(), mat[i].end(), 0);
126128
if (ans[1] < cnt) {
127-
ans[0] = i;
128-
ans[1] = cnt;
129+
ans = {i, cnt};
129130
}
130131
}
131132
return ans;
@@ -137,16 +138,14 @@ public:
137138
138139
```go
139140
func rowAndMaximumOnes(mat [][]int) []int {
140-
ans := make([]int, 2)
141+
ans := []int{0, 0}
141142
for i, row := range mat {
142143
cnt := 0
143144
for _, x := range row {
144-
if x == 1 {
145-
cnt++
146-
}
145+
cnt += x
147146
}
148147
if ans[1] < cnt {
149-
ans[0], ans[1] = i, cnt
148+
ans = []int{i, cnt}
150149
}
151150
}
152151
return ans
@@ -158,8 +157,8 @@ func rowAndMaximumOnes(mat [][]int) []int {
158157
```ts
159158
function rowAndMaximumOnes(mat: number[][]): number[] {
160159
const ans: number[] = [0, 0];
161-
for (let i = 0; i < mat.length; ++i) {
162-
const cnt = mat[i].reduce((a, b) => a + b);
160+
for (let i = 0; i < mat.length; i++) {
161+
const cnt = mat[i].reduce((sum, num) => sum + num, 0);
163162
if (ans[1] < cnt) {
164163
ans[0] = i;
165164
ans[1] = cnt;
@@ -175,20 +174,34 @@ function rowAndMaximumOnes(mat: number[][]): number[] {
175174
impl Solution {
176175
pub fn row_and_maximum_ones(mat: Vec<Vec<i32>>) -> Vec<i32> {
177176
let mut ans = vec![0, 0];
178-
179177
for (i, row) in mat.iter().enumerate() {
180-
let cnt = row.iter().filter(|&v|*v==1).count() asi32;
178+
let cnt = row.iter().sum();
181179
if ans[1] < cnt {
182-
ans[0] = i as i32;
183-
ans[1] = cnt;
180+
ans = vec![i as i32, cnt];
184181
}
185182
}
186-
187183
ans
188184
}
189185
}
190186
```
191187

188+
#### C#
189+
190+
```cs
191+
public class Solution {
192+
public int[] RowAndMaximumOnes(int[][] mat) {
193+
int[] ans = new int[2];
194+
for (int i = 0; i < mat.Length; i++) {
195+
int cnt = mat[i].Sum();
196+
if (ans[1] < cnt) {
197+
ans = new int[] { i, cnt };
198+
}
199+
}
200+
return ans;
201+
}
202+
}
203+
```
204+
192205
<!-- tabs:end -->
193206

194207
<!-- solution:end -->

‎solution/2600-2699/2643.Row With Maximum Ones/README_EN.md‎

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131
<pre>
3232
<strong>Input:</strong> mat = [[0,1],[1,0]]
3333
<strong>Output:</strong> [0,1]
34-
<strong>Explanation:</strong> Both rows have the same number of 1&#39;s. So we return the index of the smaller row, 0, and the maximum count of ones (1<code>)</code>. So, the answer is [0,1].
34+
<strong>Explanation:</strong> Both rows have the same number of 1&#39;s. So we return the index of the smaller row, 0, and the maximum count of ones (1<code>)</code>. So, the answer is [0,1].
3535
</pre>
3636

3737
<p><strong class="example">Example 2:</strong></p>
@@ -68,9 +68,16 @@ tags:
6868

6969
### Solution 1: Simulation
7070

71-
We directly traverse the matrix, count the number of 1ドル$s in each row, and update the maximum value and the corresponding row index. Note that if the number of 1ドル$s in the current row is equal to the maximum value, we need to choose the row with the smaller index.
71+
We initialize an array $\textit{ans} = [0, 0]$ to store the index of the row with the most 1ドル$s and the count of 1ドル$s.
7272

73-
The time complexity is $O(m \times n),ドル where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
73+
Then, we iterate through each row of the matrix:
74+
75+
- Compute the number of 1ドル$s in the current row, denoted as $\textit{cnt}$ (since the matrix contains only 0ドル$s and 1ドル$s, we can directly sum up the row).
76+
- If $\textit{ans}[1] < \textit{cnt},ドル update $\textit{ans} = [i, \textit{cnt}]$.
77+
78+
After finishing the iteration, we return $\textit{ans}$.
79+
80+
The time complexity is $O(m \times n),ドル where $m$ and $n$ are the number of rows and columns in the matrix, respectively. The space complexity is $O(1)$.
7481

7582
<!-- tabs:start -->
7683

@@ -81,7 +88,7 @@ class Solution:
8188
def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
8289
ans = [0, 0]
8390
for i, row in enumerate(mat):
84-
cnt = row.count(1)
91+
cnt = sum(row)
8592
if ans[1] < cnt:
8693
ans = [i, cnt]
8794
return ans
@@ -96,9 +103,7 @@ class Solution {
96103
for (int i = 0; i < mat.length; ++i) {
97104
int cnt = 0;
98105
for (int x : mat[i]) {
99-
if (x == 1) {
100-
++cnt;
101-
}
106+
cnt += x;
102107
}
103108
if (ans[1] < cnt) {
104109
ans[0] = i;
@@ -118,13 +123,9 @@ public:
118123
vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
119124
vector<int> ans(2);
120125
for (int i = 0; i < mat.size(); ++i) {
121-
int cnt = 0;
122-
for (auto& x : mat[i]) {
123-
cnt += x == 1;
124-
}
126+
int cnt = accumulate(mat[i].begin(), mat[i].end(), 0);
125127
if (ans[1] < cnt) {
126-
ans[0] = i;
127-
ans[1] = cnt;
128+
ans = {i, cnt};
128129
}
129130
}
130131
return ans;
@@ -136,16 +137,14 @@ public:
136137
137138
```go
138139
func rowAndMaximumOnes(mat [][]int) []int {
139-
ans := make([]int, 2)
140+
ans := []int{0, 0}
140141
for i, row := range mat {
141142
cnt := 0
142143
for _, x := range row {
143-
if x == 1 {
144-
cnt++
145-
}
144+
cnt += x
146145
}
147146
if ans[1] < cnt {
148-
ans[0], ans[1] = i, cnt
147+
ans = []int{i, cnt}
149148
}
150149
}
151150
return ans
@@ -157,8 +156,8 @@ func rowAndMaximumOnes(mat [][]int) []int {
157156
```ts
158157
function rowAndMaximumOnes(mat: number[][]): number[] {
159158
const ans: number[] = [0, 0];
160-
for (let i = 0; i < mat.length; ++i) {
161-
const cnt = mat[i].reduce((a, b) => a + b);
159+
for (let i = 0; i < mat.length; i++) {
160+
const cnt = mat[i].reduce((sum, num) => sum + num, 0);
162161
if (ans[1] < cnt) {
163162
ans[0] = i;
164163
ans[1] = cnt;
@@ -174,20 +173,34 @@ function rowAndMaximumOnes(mat: number[][]): number[] {
174173
impl Solution {
175174
pub fn row_and_maximum_ones(mat: Vec<Vec<i32>>) -> Vec<i32> {
176175
let mut ans = vec![0, 0];
177-
178176
for (i, row) in mat.iter().enumerate() {
179-
let cnt = row.iter().filter(|&v|*v==1).count() asi32;
177+
let cnt = row.iter().sum();
180178
if ans[1] < cnt {
181-
ans[0] = i as i32;
182-
ans[1] = cnt;
179+
ans = vec![i as i32, cnt];
183180
}
184181
}
185-
186182
ans
187183
}
188184
}
189185
```
190186

187+
#### C#
188+
189+
```cs
190+
public class Solution {
191+
public int[] RowAndMaximumOnes(int[][] mat) {
192+
int[] ans = new int[2];
193+
for (int i = 0; i < mat.Length; i++) {
194+
int cnt = mat[i].Sum();
195+
if (ans[1] < cnt) {
196+
ans = new int[] { i, cnt };
197+
}
198+
}
199+
return ans;
200+
}
201+
}
202+
```
203+
191204
<!-- tabs:end -->
192205

193206
<!-- solution:end -->

‎solution/2600-2699/2643.Row With Maximum Ones/Solution.cpp‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ class Solution {
33
vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
44
vector<int> ans(2);
55
for (int i = 0; i < mat.size(); ++i) {
6-
int cnt = 0;
7-
for (auto& x : mat[i]) {
8-
cnt += x == 1;
9-
}
6+
int cnt = accumulate(mat[i].begin(), mat[i].end(), 0);
107
if (ans[1] < cnt) {
11-
ans[0] = i;
12-
ans[1] = cnt;
8+
ans = {i, cnt};
139
}
1410
}
1511
return ans;
1612
}
17-
};
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Solution {
2+
public int[] RowAndMaximumOnes(int[][] mat) {
3+
int[] ans = new int[2];
4+
for (int i = 0; i < mat.Length; i++) {
5+
int cnt = mat[i].Sum();
6+
if (ans[1] < cnt) {
7+
ans = new int[] { i, cnt };
8+
}
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
func rowAndMaximumOnes(mat [][]int) []int {
2-
ans := make([]int, 2)
2+
ans := []int{0, 0}
33
for i, row := range mat {
44
cnt := 0
55
for _, x := range row {
6-
if x == 1 {
7-
cnt++
8-
}
6+
cnt += x
97
}
108
if ans[1] < cnt {
11-
ans[0], ans[1] =i, cnt
9+
ans= []int{i, cnt}
1210
}
1311
}
1412
return ans
15-
}
13+
}

‎solution/2600-2699/2643.Row With Maximum Ones/Solution.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ public int[] rowAndMaximumOnes(int[][] mat) {
44
for (int i = 0; i < mat.length; ++i) {
55
int cnt = 0;
66
for (int x : mat[i]) {
7-
if (x == 1) {
8-
++cnt;
9-
}
7+
cnt += x;
108
}
119
if (ans[1] < cnt) {
1210
ans[0] = i;
@@ -15,4 +13,4 @@ public int[] rowAndMaximumOnes(int[][] mat) {
1513
}
1614
return ans;
1715
}
18-
}
16+
}

‎solution/2600-2699/2643.Row With Maximum Ones/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution:
22
def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
33
ans = [0, 0]
44
for i, row in enumerate(mat):
5-
cnt = row.count(1)
5+
cnt = sum(row)
66
if ans[1] < cnt:
77
ans = [i, cnt]
88
return ans
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
impl Solution {
22
pub fn row_and_maximum_ones(mat: Vec<Vec<i32>>) -> Vec<i32> {
33
let mut ans = vec![0, 0];
4-
54
for (i, row) in mat.iter().enumerate() {
6-
let cnt = row.iter().filter(|&v| *v == 1).count()asi32;
5+
let cnt = row.iter().sum();
76
if ans[1] < cnt {
8-
ans[0] = i as i32;
9-
ans[1] = cnt;
7+
ans = vec![i as i32, cnt];
108
}
119
}
12-
1310
ans
1411
}
1512
}

‎solution/2600-2699/2643.Row With Maximum Ones/Solution.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function rowAndMaximumOnes(mat: number[][]): number[] {
22
const ans: number[] = [0, 0];
3-
for (let i = 0; i < mat.length; ++i) {
4-
const cnt = mat[i].reduce((a,b) => a + b);
3+
for (let i = 0; i < mat.length; i++) {
4+
const cnt = mat[i].reduce((sum,num) => sum + num,0);
55
if (ans[1] < cnt) {
66
ans[0] = i;
77
ans[1] = cnt;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /