diff --git a/solution/2600-2699/2643.Row With Maximum Ones/README.md b/solution/2600-2699/2643.Row With Maximum Ones/README.md
index d29d5170fab3a..1d6d883d7d79f 100644
--- a/solution/2600-2699/2643.Row With Maximum Ones/README.md
+++ b/solution/2600-2699/2643.Row With Maximum Ones/README.md
@@ -32,7 +32,7 @@ tags:
输入:mat = [[0,1],[1,0]]
输出:[0,1]
-解释:两行中 1 的数量相同。所以返回下标最小的行,下标为 0 。该行 1 的数量为 1 。所以,答案为 [0,1] 。
+解释:两行中 1 的数量相同。所以返回下标最小的行,下标为 0 。该行 1 的数量为 1 。所以,答案为 [0,1] 。
示例 2:
@@ -69,9 +69,16 @@ tags:
### 方法一:模拟
-我们直接遍历矩阵,统计每一行中 $1$ 的个数,更新最大值和对应的行下标。注意,如果当前行的 $1$ 的个数与最大值相等,我们需要选择行下标较小的那一行。
+我们初始化一个数组 $\textit{ans} = [0, 0]$,用于记录最多 $1$ 的行的下标和 $1$ 的数量。
-时间复杂度 $(m \times n)$,其中 $m$ 和 $n$ 分别为矩阵的行数和列数。空间复杂度 $O(1)$。
+然后遍历矩阵的每一行,对于每一行:
+
+- 计算该行 $1$ 的数量 $\textit{cnt}$(由于矩阵中只包含 $0$ 和 $1$,我们可以直接对该行求和);
+- 如果 $\textit{ans}[1] < \textit{cnt}$,则更新 $\textit{ans} = [i, \textit{cnt}]$。
+
+遍历结束后,返回 $\textit{ans}$。
+
+时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。
@@ -82,7 +89,7 @@ class Solution:
def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
ans = [0, 0]
for i, row in enumerate(mat):
- cnt = row.count(1)
+ cnt = sum(row)
if ans[1] < cnt:
ans = [i, cnt]
return ans
@@ -97,9 +104,7 @@ class Solution {
for (int i = 0; i < mat.length; ++i) {
int cnt = 0;
for (int x : mat[i]) {
- if (x == 1) {
- ++cnt;
- }
+ cnt += x;
}
if (ans[1] < cnt) {
ans[0] = i;
@@ -119,13 +124,9 @@ public:
vector rowAndMaximumOnes(vector>& mat) {
vector ans(2);
for (int i = 0; i < mat.size(); ++i) {
- int cnt = 0;
- for (auto& x : mat[i]) {
- cnt += x == 1;
- }
+ int cnt = accumulate(mat[i].begin(), mat[i].end(), 0);
if (ans[1] < cnt) {
- ans[0] = i;
- ans[1] = cnt;
+ ans = {i, cnt};
}
}
return ans;
@@ -137,16 +138,14 @@ public:
```go
func rowAndMaximumOnes(mat [][]int) []int {
- ans := make([]int, 2)
+ ans := []int{0, 0}
for i, row := range mat {
cnt := 0
for _, x := range row {
- if x == 1 {
- cnt++
- }
+ cnt += x
}
if ans[1] < cnt {
- ans[0], ans[1] = i, cnt
+ ans = []int{i, cnt}
}
}
return ans
@@ -158,8 +157,8 @@ func rowAndMaximumOnes(mat [][]int) []int {
```ts
function rowAndMaximumOnes(mat: number[][]): number[] {
const ans: number[] = [0, 0];
- for (let i = 0; i < mat.length; ++i) {
- const cnt = mat[i].reduce((a, b) => a + b);
+ for (let i = 0; i < mat.length; i++) {
+ const cnt = mat[i].reduce((sum, num) => sum + num, 0);
if (ans[1] < cnt) {
ans[0] = i;
ans[1] = cnt;
@@ -175,20 +174,34 @@ function rowAndMaximumOnes(mat: number[][]): number[] {
impl Solution {
pub fn row_and_maximum_ones(mat: Vec>) -> Vec {
let mut ans = vec![0, 0];
-
for (i, row) in mat.iter().enumerate() {
- let cnt = row.iter().filter(|&v| *v == 1).count() as i32;
+ let cnt = row.iter().sum();
if ans[1] < cnt {
- ans[0] = i as i32;
- ans[1] = cnt;
+ ans = vec![i as i32, cnt];
}
}
-
ans
}
}
```
+#### C#
+
+```cs
+public class Solution {
+ public int[] RowAndMaximumOnes(int[][] mat) {
+ int[] ans = new int[2];
+ for (int i = 0; i < mat.Length; i++) {
+ int cnt = mat[i].Sum();
+ if (ans[1] < cnt) {
+ ans = new int[] { i, cnt };
+ }
+ }
+ return ans;
+ }
+}
+```
+
diff --git a/solution/2600-2699/2643.Row With Maximum Ones/README_EN.md b/solution/2600-2699/2643.Row With Maximum Ones/README_EN.md
index 236220b1491cd..f6b88dcb4e3eb 100644
--- a/solution/2600-2699/2643.Row With Maximum Ones/README_EN.md
+++ b/solution/2600-2699/2643.Row With Maximum Ones/README_EN.md
@@ -31,7 +31,7 @@ tags:
Input: mat = [[0,1],[1,0]]
Output: [0,1]
-Explanation: Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1)
. So, the answer is [0,1].
+Explanation: Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1)
. So, the answer is [0,1].
Example 2:
@@ -68,9 +68,16 @@ tags:
### Solution 1: Simulation
-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.
+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.
-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)$.
+Then, we iterate through each row of the matrix:
+
+- 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).
+- If $\textit{ans}[1] < \textit{cnt}$, update $\textit{ans} = [i, \textit{cnt}]$.
+
+After finishing the iteration, we return $\textit{ans}$.
+
+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)$.
@@ -81,7 +88,7 @@ class Solution:
def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
ans = [0, 0]
for i, row in enumerate(mat):
- cnt = row.count(1)
+ cnt = sum(row)
if ans[1] < cnt:
ans = [i, cnt]
return ans
@@ -96,9 +103,7 @@ class Solution {
for (int i = 0; i < mat.length; ++i) {
int cnt = 0;
for (int x : mat[i]) {
- if (x == 1) {
- ++cnt;
- }
+ cnt += x;
}
if (ans[1] < cnt) {
ans[0] = i;
@@ -118,13 +123,9 @@ public:
vector rowAndMaximumOnes(vector>& mat) {
vector ans(2);
for (int i = 0; i < mat.size(); ++i) {
- int cnt = 0;
- for (auto& x : mat[i]) {
- cnt += x == 1;
- }
+ int cnt = accumulate(mat[i].begin(), mat[i].end(), 0);
if (ans[1] < cnt) {
- ans[0] = i;
- ans[1] = cnt;
+ ans = {i, cnt};
}
}
return ans;
@@ -136,16 +137,14 @@ public:
```go
func rowAndMaximumOnes(mat [][]int) []int {
- ans := make([]int, 2)
+ ans := []int{0, 0}
for i, row := range mat {
cnt := 0
for _, x := range row {
- if x == 1 {
- cnt++
- }
+ cnt += x
}
if ans[1] < cnt {
- ans[0], ans[1] = i, cnt
+ ans = []int{i, cnt}
}
}
return ans
@@ -157,8 +156,8 @@ func rowAndMaximumOnes(mat [][]int) []int {
```ts
function rowAndMaximumOnes(mat: number[][]): number[] {
const ans: number[] = [0, 0];
- for (let i = 0; i < mat.length; ++i) {
- const cnt = mat[i].reduce((a, b) => a + b);
+ for (let i = 0; i < mat.length; i++) {
+ const cnt = mat[i].reduce((sum, num) => sum + num, 0);
if (ans[1] < cnt) {
ans[0] = i;
ans[1] = cnt;
@@ -174,20 +173,34 @@ function rowAndMaximumOnes(mat: number[][]): number[] {
impl Solution {
pub fn row_and_maximum_ones(mat: Vec>) -> Vec {
let mut ans = vec![0, 0];
-
for (i, row) in mat.iter().enumerate() {
- let cnt = row.iter().filter(|&v| *v == 1).count() as i32;
+ let cnt = row.iter().sum();
if ans[1] < cnt {
- ans[0] = i as i32;
- ans[1] = cnt;
+ ans = vec![i as i32, cnt];
}
}
-
ans
}
}
```
+#### C#
+
+```cs
+public class Solution {
+ public int[] RowAndMaximumOnes(int[][] mat) {
+ int[] ans = new int[2];
+ for (int i = 0; i < mat.Length; i++) {
+ int cnt = mat[i].Sum();
+ if (ans[1] < cnt) {
+ ans = new int[] { i, cnt };
+ }
+ }
+ return ans;
+ }
+}
+```
+
diff --git a/solution/2600-2699/2643.Row With Maximum Ones/Solution.cpp b/solution/2600-2699/2643.Row With Maximum Ones/Solution.cpp
index 79bcbe90b4e44..5df58522c7b0a 100644
--- a/solution/2600-2699/2643.Row With Maximum Ones/Solution.cpp
+++ b/solution/2600-2699/2643.Row With Maximum Ones/Solution.cpp
@@ -3,15 +3,11 @@ class Solution {
vector rowAndMaximumOnes(vector>& mat) {
vector ans(2);
for (int i = 0; i < mat.size(); ++i) {
- int cnt = 0;
- for (auto& x : mat[i]) {
- cnt += x == 1;
- }
+ int cnt = accumulate(mat[i].begin(), mat[i].end(), 0);
if (ans[1] < cnt) {
- ans[0] = i;
- ans[1] = cnt;
+ ans = {i, cnt};
}
}
return ans;
}
-};
\ No newline at end of file
+};
diff --git a/solution/2600-2699/2643.Row With Maximum Ones/Solution.cs b/solution/2600-2699/2643.Row With Maximum Ones/Solution.cs
new file mode 100644
index 0000000000000..1b3b9999f632a
--- /dev/null
+++ b/solution/2600-2699/2643.Row With Maximum Ones/Solution.cs
@@ -0,0 +1,12 @@
+public class Solution {
+ public int[] RowAndMaximumOnes(int[][] mat) {
+ int[] ans = new int[2];
+ for (int i = 0; i < mat.Length; i++) {
+ int cnt = mat[i].Sum();
+ if (ans[1] < cnt) {
+ ans = new int[] { i, cnt };
+ }
+ }
+ return ans;
+ }
+}
diff --git a/solution/2600-2699/2643.Row With Maximum Ones/Solution.go b/solution/2600-2699/2643.Row With Maximum Ones/Solution.go
index e3b9d532c6c55..8951c9e9b1aa4 100644
--- a/solution/2600-2699/2643.Row With Maximum Ones/Solution.go
+++ b/solution/2600-2699/2643.Row With Maximum Ones/Solution.go
@@ -1,15 +1,13 @@
func rowAndMaximumOnes(mat [][]int) []int {
- ans := make([]int, 2)
+ ans := []int{0, 0}
for i, row := range mat {
cnt := 0
for _, x := range row {
- if x == 1 {
- cnt++
- }
+ cnt += x
}
if ans[1] < cnt {
- ans[0], ans[1] = i, cnt
+ ans = []int{i, cnt}
}
}
return ans
-}
\ No newline at end of file
+}
diff --git a/solution/2600-2699/2643.Row With Maximum Ones/Solution.java b/solution/2600-2699/2643.Row With Maximum Ones/Solution.java
index 1658ecd1f784d..0c3880cb7020a 100644
--- a/solution/2600-2699/2643.Row With Maximum Ones/Solution.java
+++ b/solution/2600-2699/2643.Row With Maximum Ones/Solution.java
@@ -4,9 +4,7 @@ public int[] rowAndMaximumOnes(int[][] mat) {
for (int i = 0; i < mat.length; ++i) {
int cnt = 0;
for (int x : mat[i]) {
- if (x == 1) {
- ++cnt;
- }
+ cnt += x;
}
if (ans[1] < cnt) {
ans[0] = i;
@@ -15,4 +13,4 @@ public int[] rowAndMaximumOnes(int[][] mat) {
}
return ans;
}
-}
\ No newline at end of file
+}
diff --git a/solution/2600-2699/2643.Row With Maximum Ones/Solution.py b/solution/2600-2699/2643.Row With Maximum Ones/Solution.py
index af13862cc0378..860faeeac4ce2 100644
--- a/solution/2600-2699/2643.Row With Maximum Ones/Solution.py
+++ b/solution/2600-2699/2643.Row With Maximum Ones/Solution.py
@@ -2,7 +2,7 @@ class Solution:
def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
ans = [0, 0]
for i, row in enumerate(mat):
- cnt = row.count(1)
+ cnt = sum(row)
if ans[1] < cnt:
ans = [i, cnt]
return ans
diff --git a/solution/2600-2699/2643.Row With Maximum Ones/Solution.rs b/solution/2600-2699/2643.Row With Maximum Ones/Solution.rs
index da124bf28e74f..9f88929a8d6fa 100644
--- a/solution/2600-2699/2643.Row With Maximum Ones/Solution.rs
+++ b/solution/2600-2699/2643.Row With Maximum Ones/Solution.rs
@@ -1,15 +1,12 @@
impl Solution {
pub fn row_and_maximum_ones(mat: Vec>) -> Vec {
let mut ans = vec![0, 0];
-
for (i, row) in mat.iter().enumerate() {
- let cnt = row.iter().filter(|&v| *v == 1).count() as i32;
+ let cnt = row.iter().sum();
if ans[1] < cnt {
- ans[0] = i as i32;
- ans[1] = cnt;
+ ans = vec![i as i32, cnt];
}
}
-
ans
}
}
diff --git a/solution/2600-2699/2643.Row With Maximum Ones/Solution.ts b/solution/2600-2699/2643.Row With Maximum Ones/Solution.ts
index 879c1afd9b73a..2d8f1513c806e 100644
--- a/solution/2600-2699/2643.Row With Maximum Ones/Solution.ts
+++ b/solution/2600-2699/2643.Row With Maximum Ones/Solution.ts
@@ -1,7 +1,7 @@
function rowAndMaximumOnes(mat: number[][]): number[] {
const ans: number[] = [0, 0];
- for (let i = 0; i < mat.length; ++i) {
- const cnt = mat[i].reduce((a, b) => a + b);
+ for (let i = 0; i < mat.length; i++) {
+ const cnt = mat[i].reduce((sum, num) => sum + num, 0);
if (ans[1] < cnt) {
ans[0] = i;
ans[1] = cnt;