[LeetCode] Row With Maximum Ones

2643. Row With Maximum Ones

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
vector<int> rowAndMaximumOnes(vector<vector<int>>& mat) {
int c= count(begin(mat[0]), end(mat[0]),1);
vector<int> res{0,c};
for(int i = 1; i < mat.size(); i++) {
int c = count(begin(mat[i]), end(mat[i]), 1);
if(res[1] < c) res = {i,c};
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/16/PS/LeetCode/row-with-maximum-ones/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.