返回介绍

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

发布于 2024-06-17 01:03:01 字数 4276 浏览 0 评论 0 收藏 0

2643. Row With Maximum Ones

中文文档

Description

Given a m x n binary matrix mat, find the 0-indexed position of the row that contains the maximum count of ones, and the number of ones in that row.

In case there are multiple rows that have the maximum count of ones, the row with the smallest row number should be selected.

Return_ an array containing the index of the row, and the number of ones in it._

 

Example 1:

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]. 

Example 2:

Input: mat = [[0,0,0],[0,1,1]]
Output: [1,2]
Explanation: The row indexed 1 has the maximum count of ones (2). So we return its index, 1, and the count. So, the answer is [1,2].

Example 3:

Input: mat = [[0,0],[1,1],[0,0]]
Output: [1,2]
Explanation: The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].

 

Constraints:

  • m == mat.length 
  • n == mat[i].length 
  • 1 <= m, n <= 100 
  • mat[i][j] is either 0 or 1.

Solutions

Solution 1

class Solution:
  def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
    ans = [0, 0]
    for i, row in enumerate(mat):
      cnt = row.count(1)
      if ans[1] < cnt:
        ans = [i, cnt]
    return ans
class Solution {
  public int[] rowAndMaximumOnes(int[][] mat) {
    int[] ans = new int[2];
    for (int i = 0; i < mat.length; ++i) {
      int cnt = 0;
      for (int x : mat[i]) {
        if (x == 1) {
          ++cnt;
        }
      }
      if (ans[1] < cnt) {
        ans[0] = i;
        ans[1] = cnt;
      }
    }
    return ans;
  }
}
class Solution {
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;
      }
      if (ans[1] < cnt) {
        ans[0] = i;
        ans[1] = cnt;
      }
    }
    return ans;
  }
};
func rowAndMaximumOnes(mat [][]int) []int {
  ans := make([]int, 2)
  for i, row := range mat {
    cnt := 0
    for _, x := range row {
      if x == 1 {
        cnt++
      }
    }
    if ans[1] < cnt {
      ans[0], ans[1] = i, cnt
    }
  }
  return ans
}
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);
    if (ans[1] < cnt) {
      ans[0] = i;
      ans[1] = cnt;
    }
  }
  return ans;
}
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;
      if ans[1] < cnt {
        ans[0] = i as i32;
        ans[1] = cnt;
      }
    }

    ans
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文