Skip to content

Commit 2d0fb15

Browse files
solves #2643: RowWithMaximumOnes in java
1 parent 63ba174 commit 2d0fb15

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@
814814
| 2609 | [Find the Longest Balanced Substring of a Binary String](https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string) | [![Java](assets/java.png)](src/FindTheLongestBalancedSubstringOfABinaryString.java) | |
815815
| 2614 | [Prime In Diagonal](https://leetcode.com/problems/prime-in-diagonal) | [![Java](assets/java.png)](src/PrimeInDiagonal.java) | |
816816
| 2639 | [Find the Width of Columns of a Grid](https://leetcode.com/problems/find-the-width-of-columns-of-a-grid) | [![Java](assets/java.png)](src/FindTheWidthOfColumnsOfAGrid.java) | |
817-
| 2643 | [Row With Maximum Ones](https://leetcode.com/problems/row-with-maximum-ones) | | |
817+
| 2643 | [Row With Maximum Ones](https://leetcode.com/problems/row-with-maximum-ones) | [![Java](assets/java.png)](src/RowWithMaximumOnes.java) | |
818818
| 2644 | [Find the Maximum Divisibility Score](https://leetcode.com/problems/find-the-maximum-divisibility-score) | | |
819819
| 2651 | [Calculate Delayed Arrival Time](https://leetcode.com/problems/calculate-delayed-arrival-time) | | |
820820
| 2652 | [Sum Multiples](https://leetcode.com/problems/sum-multiples) | | |

src/RowWithMaximumOnes.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// https://leetcode.com/problems/row-with-maximum-ones
2+
// m: number of rows in matrix
3+
// n: number of columns in matrix
4+
// T: O(m * n)
5+
// S: O(m * n)
6+
7+
public class RowWithMaximumOnes {
8+
public int[] rowAndMaximumOnes(int[][] matrix) {
9+
final int rows = matrix.length, columns = matrix[0].length;
10+
int maxOnes = 0, maxRow = 0;
11+
for (int row = 0 ; row < rows ; row++) {
12+
final int onesCount = getOnesCount(matrix[row]);
13+
if (onesCount > maxOnes) {
14+
maxOnes = onesCount;
15+
maxRow = row;
16+
}
17+
}
18+
return new int[] { maxRow, maxOnes };
19+
}
20+
21+
private int getOnesCount(int[] array) {
22+
int count = 0;
23+
for (int element : array) {
24+
if (element == 1) count++;
25+
}
26+
return count;
27+
}
28+
}

0 commit comments

Comments
 (0)