Skip to content

Commit 4b8cb65

Browse files
solves count negative number in sorted matrix
1 parent bdbde9c commit 4b8cb65

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@
345345
| 1337 | [The K Weakest Rows In A Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix) | [![Java](assets/java.png)](src/TheKWeakestRowsInAMatrix.java) | |
346346
| 1342 | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero) | [![Java](assets/java.png)](src/NumberOfStepsToReduceANumberToZero.java) | |
347347
| 1346 | [Check if N and It's Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist) | [![Java](assets/java.png)](src/CheckIfNAndItsDoubleExist.java) [![Python](assets/python.png)](python/check_if_n_and_its_double_exist.py) | |
348-
| 1351 | [Count Negative Numbers In A Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix) | | |
348+
| 1351 | [Count Negative Numbers In A Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix) | [![Java](assets/java.png)](src/CountNegativeNumbersInSortedMatrix.java) | |
349349
| 1356 | [Sort Integers by Number of 1 Bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits) | | |
350350
| 1360 | [Number of Days Between Two Dates](https://leetcode.com/problems/number-of-days-between-two-dates) | | |
351351
| 1365 | [How Many Numbers Are Smaller Than Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class CountNegativeNumbersInSortedMatrix {
2+
public int countNegatives(int[][] grid) {
3+
final int rows = grid.length, columns = grid[0].length;
4+
int negativeNumbers = 0, negativeColumnIndex = columns;
5+
for (int[] row : grid) {
6+
negativeNumbers += columns;
7+
for (int column = 0 ; column < negativeColumnIndex ; column++) {
8+
if (row[column] < 0) {
9+
negativeColumnIndex = column;
10+
break;
11+
}
12+
negativeNumbers--;
13+
}
14+
}
15+
return negativeNumbers;
16+
}
17+
}

0 commit comments

Comments
 (0)