Skip to content

feat: add solutions to lc problem: No.2643 #4279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions solution/2600-2699/2643.Row With Maximum Ones/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tags:
<pre>
<strong>输入:</strong>mat = [[0,1],[1,0]]
<strong>输出:</strong>[0,1]
<strong>解释:</strong>两行中 1 的数量相同。所以返回下标最小的行,下标为 0 。该行 1 的数量为 1 。所以,答案为 [0,1] 。
<strong>解释:</strong>两行中 1 的数量相同。所以返回下标最小的行,下标为 0 。该行 1 的数量为 1 。所以,答案为 [0,1] 。
</pre>

<p><strong>示例 2:</strong></p>
Expand Down Expand Up @@ -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)$。

<!-- tabs:start -->

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -119,13 +124,9 @@ public:
vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
vector<int> 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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -175,20 +174,34 @@ function rowAndMaximumOnes(mat: number[][]): number[] {
impl Solution {
pub fn row_and_maximum_ones(mat: Vec<Vec<i32>>) -> Vec<i32> {
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;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
63 changes: 38 additions & 25 deletions solution/2600-2699/2643.Row With Maximum Ones/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:
<pre>
<strong>Input:</strong> mat = [[0,1],[1,0]]
<strong>Output:</strong> [0,1]
<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].
<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].
</pre>

<p><strong class="example">Example 2:</strong></p>
Expand Down Expand Up @@ -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)$.

<!-- tabs:start -->

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -118,13 +123,9 @@ public:
vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
vector<int> 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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -174,20 +173,34 @@ function rowAndMaximumOnes(mat: number[][]): number[] {
impl Solution {
pub fn row_and_maximum_ones(mat: Vec<Vec<i32>>) -> Vec<i32> {
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;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
10 changes: 3 additions & 7 deletions solution/2600-2699/2643.Row With Maximum Ones/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ class Solution {
vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
vector<int> 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;
}
};
};
12 changes: 12 additions & 0 deletions solution/2600-2699/2643.Row With Maximum Ones/Solution.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 4 additions & 6 deletions solution/2600-2699/2643.Row With Maximum Ones/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 2 additions & 4 deletions solution/2600-2699/2643.Row With Maximum Ones/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,4 +13,4 @@ public int[] rowAndMaximumOnes(int[][] mat) {
}
return ans;
}
}
}
2 changes: 1 addition & 1 deletion solution/2600-2699/2643.Row With Maximum Ones/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions solution/2600-2699/2643.Row With Maximum Ones/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
impl Solution {
pub fn row_and_maximum_ones(mat: Vec<Vec<i32>>) -> Vec<i32> {
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
}
}
4 changes: 2 additions & 2 deletions solution/2600-2699/2643.Row With Maximum Ones/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down