Skip to content

2643. Row With Maximum Ones 👍

  • Time: $O(mn)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
    vector<int> ans{0, 0};

    for (int i = 0; i < mat.size(); ++i) {
      const int ones = ranges::count(mat[i], 1);
      if (ones > ans[1]) {
        ans[0] = i;
        ans[1] = ones;
      }
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
  public int[] rowAndMaximumOnes(int[][] mat) {
    int[] ans = new int[2];

    for (int i = 0; i < mat.length; ++i) {
      final int ones = (int) Arrays.stream(mat[i]).filter(a -> a == 1).count();
      if (ones > ans[1]) {
        ans[0] = i;
        ans[1] = ones;
      }
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
  def rowAndMaximumOnes(self, mat: list[list[int]]) -> list[int]:
    ans = [0, 0]

    for i, row in enumerate(mat):
      ones = row.count(1)
      if ones > ans[1]:
        ans[0] = i
        ans[1] = ones

    return ans