2643. Row With Maximum Ones LeetCode Solution

In this guide, you will get 2643. Row With Maximum Ones LeetCode Solution with the best time and space complexity. The solution to Row With Maximum Ones problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Row With Maximum Ones solution in C++
  4. Row With Maximum Ones solution in Java
  5. Row With Maximum Ones solution in Python
  6. Additional Resources
2643. Row With Maximum Ones LeetCode Solution image

Problem Statement of 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.

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

See also  24. Swap Nodes in Pairs LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(1)

2643. Row With Maximum Ones LeetCode Solution in C++

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;
  }
};
/* code provided by PROGIEZ */

2643. Row With Maximum Ones LeetCode Solution in Java

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;
  }
}
// code provided by PROGIEZ

2643. Row With Maximum Ones LeetCode Solution in Python

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
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.