Skip to content

Commit 0927107

Browse files
chore: add LeetCode daily solution
1 parent 00ade88 commit 0927107

5 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Count Negative Numbers in a Sorted Matrix (Easy)
2+
3+
**Problem ID:** 1351
4+
**Date:** 2025-12-28
5+
**Link:** https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/
6+
7+
## Approach
8+
9+
To solve the problem of counting negative numbers in a sorted matrix, we can leverage the properties of the matrix's sorted order. The matrix is sorted in non-increasing order both row-wise and column-wise, which means that as we move right in a row or down in a column, the values do not increase.
10+
11+
### Approach:
12+
13+
1. **Start from the Bottom-Left Corner**:
14+
- Begin at the bottom-left corner of the matrix (i.e., the last row and the first column). This position allows us to efficiently navigate the matrix based on the values we encounter.
15+
16+
2. **Traverse the Matrix**:
17+
- If the current number is negative, all numbers above it in the same column will also be negative (due to the sorted property). Therefore, we can count all the remaining elements in that column as negative and move one step to the right to check the next column.
18+
- If the current number is non-negative, it indicates that all numbers to the left in the same row are also non-negative. Thus, we move one step up to check the next row.
19+
20+
3. **Count Negatives**:
21+
- Maintain a counter to keep track of the total number of negative numbers encountered during the traversal.
22+
23+
### Data Structures:
24+
- A simple integer counter to keep track of the count of negative numbers.
25+
- No additional data structures are needed since we are iterating through the matrix directly.
26+
27+
### Complexity:
28+
- **Time Complexity**: O(n + m), where n is the number of rows and m is the number of columns. In the worst case, we traverse each row and column once.
29+
- **Space Complexity**: O(1), as we are using only a fixed amount of space for the counter and no additional data structures.
30+
31+
This approach efficiently counts the negative numbers while taking full advantage of the matrix's sorted properties, ensuring that we meet the follow-up requirement of achieving O(n + m) time complexity.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int countNegatives(int[][] grid) {
3+
int m = grid.length;
4+
int n = grid[0].length;
5+
int count = 0;
6+
int row = m - 1;
7+
int col = 0;
8+
9+
while (row >= 0 && col < n) {
10+
if (grid[row][col] < 0) {
11+
count += (n - col);
12+
row--;
13+
} else {
14+
col++;
15+
}
16+
}
17+
18+
return count;
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var countNegatives = function(grid) {
2+
let count = 0;
3+
const m = grid.length;
4+
const n = grid[0].length;
5+
let row = m - 1;
6+
let col = 0;
7+
8+
while (row >= 0 && col < n) {
9+
if (grid[row][col] < 0) {
10+
count += n - col;
11+
row--;
12+
} else {
13+
col++;
14+
}
15+
}
16+
17+
return count;
18+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def countNegatives(self, grid: List[List[int]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
count = 0
5+
row, col = m - 1, 0
6+
7+
while row >= 0 and col < n:
8+
if grid[row][col] < 0:
9+
count += n - col
10+
row -= 1
11+
else:
12+
col += 1
13+
14+
return count

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
318318
- 2025-12-25 — [Maximize Happiness of Selected Children](https://leetcode.com/problems/maximize-happiness-of-selected-children/) (Medium) → `Medium/2025-12-25-3075-Maximize-Happiness-of-Selected-Children`
319319
- 2025-12-26 — [Minimum Penalty for a Shop](https://leetcode.com/problems/minimum-penalty-for-a-shop/) (Medium) → `Medium/2025-12-26-2483-Minimum-Penalty-for-a-Shop`
320320
- 2025-12-27 — [Meeting Rooms III](https://leetcode.com/problems/meeting-rooms-iii/) (Hard) → `Hard/2025-12-27-2402-Meeting-Rooms-III`
321+
- 2025-12-28 — [Count Negative Numbers in a Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/) (Easy) → `Easy/2025-12-28-1351-Count-Negative-Numbers-in-a-Sorted-Matrix`

0 commit comments

Comments
 (0)