diff options
Diffstat (limited to 'row_max_ones.py')
-rw-r--r-- | row_max_ones.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/row_max_ones.py b/row_max_ones.py new file mode 100644 index 0000000..2b0004e --- /dev/null +++ b/row_max_ones.py @@ -0,0 +1,36 @@ +# 2643. Row With Maximum Ones + +""" +given an 'm * 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 number should be selected. return an array containing the +index of the row and the number of ones in it +""" + + +class Solution(object): + def rowAndMaximumOnes(self, mat): + """ + :type mat: List[List[int]] + :rtype: List[int] + """ + cnt, ans = 0, [] + ans.append(0) + ans.append(0) + for row in range(len(mat)): + for col in mat[row]: + if col == 1: + cnt += 1 + if ans[1] < cnt: + ans[0] = row + ans[1] = cnt + cnt = 0 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.rowAndMaximumOnes(mat=[[0, 1], [1, 0]])) + print(obj.rowAndMaximumOnes(mat=[[0, 0, 0], [0, 1, 1]])) + print(obj.rowAndMaximumOnes(mat=[[0, 0], [1, 1], [0, 0]])) |