Skip to content

Commit 0ee9a55

Browse files
jeremymanninggithub-actions[bot]
authored andcommitted
Auto-solve daily LeetCode problem using GPT-5-mini
1 parent 9576813 commit 0ee9a55

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

problems/1351/gpt5-mini.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Problem 1351: Count Negative Numbers in a Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/description/?envType=daily-question)
2+
3+
## Initial thoughts (stream-of-consciousness)
4+
[The matrix is sorted in non-increasing order both row-wise (left to right decreases) and column-wise (top to bottom decreases). I need to count negatives quickly. A naive approach is to check every cell O(m*n). Since rows are sorted, in each row I could binary search the first negative index, giving O(m log n). But the follow-up asks for O(m + n). I recall a standard trick: start at the top-right corner. If that element is negative, then everything below in that column must be negative (because column is non-increasing downwards), so I can add (m - i) and move left one column. If it's non-negative, move down to next row. That walks at most m + n steps. This should be simple and constant extra space.]
5+
6+
## Refining the problem, round 2 thoughts
7+
[Check edge cases: all positives -> algorithm should return 0 (we'll just move down rows and never add). All negatives -> starting at top-right it's negative so we add m and move left repeatedly until columns exhausted, total m*n. Single row/column cases should work. Must ensure indices don't go out of range. Alternative: binary search per row (O(m log n)) is acceptable for constraints up to 100x100 but follow-up prefers O(m + n). Time complexity for two-pointer top-right approach is O(m + n), space O(1). Implementation detail: start i = 0 (row), j = n - 1 (col). While i < m and j >= 0: if grid[i][j] < 0: count += m - i; j -= 1 else: i += 1.]
8+
9+
## Attempted solution(s)
10+
```python
11+
class Solution:
12+
def countNegatives(self, grid: list[list[int]]) -> int:
13+
m = len(grid)
14+
if m == 0:
15+
return 0
16+
n = len(grid[0])
17+
i, j = 0, n - 1
18+
count = 0
19+
# Start at top-right, move left when we find negatives (adding whole column below),
20+
# move down when value is non-negative.
21+
while i < m and j >= 0:
22+
if grid[i][j] < 0:
23+
# All elements grid[i..m-1][j] are negative
24+
count += (m - i)
25+
j -= 1
26+
else:
27+
i += 1
28+
return count
29+
```
30+
- Notes:
31+
- Approach: two-pointer traversal starting at top-right achieves O(m + n) time by moving either left or down on each step.
32+
- Time complexity: O(m + n), where m = number of rows and n = number of columns.
33+
- Space complexity: O(1) additional space.
34+
- Correctness: Uses the sorted (non-increasing) property both row-wise and column-wise to count entire columns below a discovered negative in constant time per column move.

0 commit comments

Comments
 (0)