Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 73f7d1a

Browse files
Merge pull request #10 from borrelunde/problem_1351_count_negative_numbers_in_a_sorted_matrix
LeetCode problem: 1351. Count Negative Numbers in a Sorted Matrix
2 parents d815344 + f958000 commit 73f7d1a

File tree

3 files changed

+206
-0
lines changed
  • src
    • main/java/com/borrelunde/leetcodesolutions/problem1351/countnegativenumbersinasortedmatrix
    • test/java/com/borrelunde/leetcodesolutions/problem1351/countnegativenumbersinasortedmatrix

3 files changed

+206
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 1351. Count Negative Numbers in a Sorted Matrix
2+
3+
Difficulty: `Easy`
4+
Topics: `Array`, `Binary Search`, `Matrix`
5+
6+
Given a `m x n` matrix `grid` which is sorted in non-increasing order both row-wise and column-wise, return *the number
7+
of **negative** numbers in `grid`*.
8+
9+
**Example 1:**
10+
11+
```text
12+
Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
13+
Output: 8
14+
Explanation: There are 8 negatives number in the matrix.
15+
```
16+
17+
**Example 2:**
18+
19+
```text
20+
Input: grid = [[3,2],[1,0]]
21+
Output: 0
22+
```
23+
24+
**Constraints:**
25+
26+
- `m == grid.length`
27+
- `n == grid[i].length`
28+
- `1 <= m, n <= 100`
29+
- `-100 <= grid[i][j] <= 100`
30+
31+
**Follow up:** Could you find an `O(n + m)` solution?
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.borrelunde.leetcodesolutions.problem1351.countnegativenumbersinasortedmatrix;
2+
3+
/**
4+
* This is the solution to the LeetCode problem: 1351. Count Negative Numbers in
5+
* a Sorted Matrix
6+
*
7+
* @author Børre A. Opedal Lunde
8+
* @since 2024年01月30日
9+
*/
10+
public class Solution {
11+
12+
public int countNegatives(int[][] grid) {
13+
14+
// This is the sum of negative numbers that will be returned.
15+
int sum = 0;
16+
17+
// Perform a binary search in every row to find the number of negative
18+
// numbers in each. That number is added to the sum.
19+
for (final int[] row : grid) {
20+
21+
int low = 0;
22+
int high = row.length;
23+
24+
while (low < high) {
25+
26+
// Bitwise right shift (>> 1) divides by two to get the middle.
27+
final int searchIndex = low + ((high - low) >> 1);
28+
29+
// The target is (-1), the first possible negative number.
30+
if (row[searchIndex] > - 1) {
31+
low = searchIndex + 1;
32+
} else {
33+
high = searchIndex;
34+
}
35+
}
36+
37+
// Low represents the index of the first negative number in the row.
38+
// Given we know all elements after it are negative, we can use it
39+
// to calculate the number of negative numbers instead of actually
40+
// counting them.
41+
sum += row.length - low;
42+
}
43+
44+
// Return the sum of negative numbers.
45+
return sum;
46+
}
47+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.borrelunde.leetcodesolutions.problem1351.countnegativenumbersinasortedmatrix;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.Arguments;
7+
import org.junit.jupiter.params.provider.MethodSource;
8+
9+
import java.util.stream.Stream;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
/**
14+
* This is the test to the LeetCode problem: 1351. Count Negative Numbers in a
15+
* Sorted Matrix
16+
*
17+
* @author Børre A. Opedal Lunde
18+
* @since 2024年01月30日
19+
*/
20+
@DisplayName("Count Negative Numbers in a Sorted Matrix")
21+
class SolutionTest {
22+
23+
private final Solution solution = new Solution();
24+
25+
@Test
26+
@DisplayName("Example one")
27+
void exampleOne() {
28+
int[][] grid = {
29+
{4, 3, 2, - 1},
30+
{3, 2, 1, - 1},
31+
{1, 1, - 1, - 2},
32+
{- 1, - 1, - 2, - 3}
33+
};
34+
35+
int expected = 8;
36+
int actual = solution.countNegatives(grid);
37+
38+
assertEquals(expected, actual);
39+
}
40+
41+
@Test
42+
@DisplayName("Example two")
43+
void exampleTwo() {
44+
int[][] grid = {
45+
{3, 2},
46+
{1, 0}
47+
};
48+
49+
int expected = 0;
50+
int actual = solution.countNegatives(grid);
51+
52+
assertEquals(expected, actual);
53+
}
54+
55+
@Test
56+
@DisplayName("Should return zero when grid is empty")
57+
void shouldReturnZeroWhenGridIsEmpty() {
58+
int[][] grid = {{}};
59+
60+
int expected = 0;
61+
int actual = solution.countNegatives(grid);
62+
63+
assertEquals(expected, actual);
64+
}
65+
66+
@Test
67+
@DisplayName("Should return zero when grid is one positive element")
68+
void shouldReturnZeroWhenGridIsOnePositiveElement() {
69+
int[][] grid = {
70+
{1}
71+
};
72+
73+
int expected = 0;
74+
int actual = solution.countNegatives(grid);
75+
76+
assertEquals(expected, actual);
77+
}
78+
79+
@Test
80+
@DisplayName("Should return one when grid is one negative element")
81+
void shouldReturnOneWhenGridIsOneNegativeElement() {
82+
int[][] grid = {
83+
{- 1}
84+
};
85+
86+
int expected = 1;
87+
int actual = solution.countNegatives(grid);
88+
89+
assertEquals(expected, actual);
90+
}
91+
92+
static Stream<Arguments> provideGrids() {
93+
return Stream.of(
94+
Arguments.of(new int[][]{
95+
{0, 0},
96+
{0, 0}
97+
}, 0),
98+
Arguments.of(new int[][]{
99+
{2, 1},
100+
{2, 1}
101+
}, 0),
102+
Arguments.of(new int[][]{
103+
{- 1, - 1},
104+
{- 1, - 1}
105+
}, 4),
106+
Arguments.of(new int[][]{
107+
{- 1, - 2},
108+
{- 1, - 2}
109+
}, 4),
110+
Arguments.of(new int[][]{
111+
{5, 4, 3, 2, 1},
112+
{4, 3, 2, 1, 0},
113+
{3, 2, 1, 0, - 1},
114+
{2, 1, 0, - 1, - 2},
115+
{1, 0, - 1, - 2, - 3},
116+
}, 6)
117+
);
118+
}
119+
120+
@ParameterizedTest(name = "{1} negative numbers in {0}")
121+
@DisplayName("Count negative numbers in various sorted matrices")
122+
@MethodSource("provideGrids")
123+
void countNegativeNumbersInVariousSortedMatrices(int[][] grid, int expected) {
124+
int actual = solution.countNegatives(grid);
125+
126+
assertEquals(expected, actual);
127+
}
128+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /