All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2601_2700.s2643_row_with_maximum_ones.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g2601_2700.s2643_row_with_maximum_ones;

// #Easy #Array #Matrix #2023_09_05_Time_1_ms_(100.00%)_Space_44.6_MB_(89.31%)

/**
 * 2643 - Row With Maximum Ones\.
 *
 * Easy
 *
 * 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`.
**/
public class Solution {
    public int[] rowAndMaximumOnes(int[][] mat) {
        int row = -1;
        int best = -1;
        for (int i = 0; i < mat.length; i++) {
            int sum = 0;
            for (int j = 0; j < mat[i].length; j++) {
                sum += mat[i][j];
            }
            if (sum > best) {
                best = sum;
                row = i;
            }
        }
        return new int[] {row, best};
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy