Skip to content

Commit 5d9faf3

Browse files
committed
leetcode
1 parent 69e9b9a commit 5d9faf3

4 files changed

+174
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
3+
4+
-* 1351. Count Negative Numbers in a Sorted 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 of negative numbers in grid.
7+
8+
9+
10+
Example 1:
11+
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+
Example 2:
16+
17+
Input: grid = [[3,2],[1,0]]
18+
Output: 0
19+
20+
21+
Constraints:
22+
23+
m == grid.length
24+
n == grid[i].length
25+
1 <= m, n <= 100
26+
-100 <= grid[i][j] <= 100
27+
28+
29+
Follow up: Could you find an O(n + m) solution?
30+
31+
32+
33+
34+
35+
*/
36+
37+
class A {
38+
int countNegatives(List<List<int>> grid) {
39+
int count = 0;
40+
final int rows = grid.length;
41+
final int columns = grid[0].length;
42+
int row = 0;
43+
int column = columns - 1;
44+
while (row < rows && column >= 0) {
45+
if (grid[row][column] < 0) {
46+
count += (column + 1);
47+
row += 1;
48+
} else {
49+
column -= 1;
50+
}
51+
}
52+
return count;
53+
}
54+
}
55+
56+
class B {
57+
int countNegatives(List<List<int>> grid) {
58+
int count = 0;
59+
final int rows = grid.length;
60+
final int cols = grid[0].length;
61+
int row = rows - 1;
62+
int col = 0;
63+
64+
while (col < cols && row >= 0) {
65+
if (grid[row][col] < 0) {
66+
count += cols - col;
67+
row--;
68+
} else {
69+
col++;
70+
}
71+
}
72+
73+
return count;
74+
}
75+
}
76+
77+
class C {
78+
int countNegatives(List<List<int>> grid) {
79+
final int gridRowsLength = grid.length;
80+
int count = 0;
81+
82+
for (int i = 0; i < gridRowsLength; i++) {
83+
grid[i].sort();
84+
final int upperBoundCalculation = binarySearch(grid[i], -1);
85+
count += grid[i].length - upperBoundCalculation;
86+
}
87+
88+
return count;
89+
}
90+
91+
int binarySearch(List<int> list, int target) {
92+
int start = 0;
93+
int end = list.length;
94+
95+
while (start < end) {
96+
final int mid = start + ((end - start) ~/ 2);
97+
if (list[mid] > target) {
98+
start = mid + 1;
99+
} else {
100+
end = mid;
101+
}
102+
}
103+
104+
return start;
105+
}
106+
}
107+
108+
class Solution {
109+
int countNegatives(List<List<int>> grid) {
110+
int count = 0;
111+
112+
for (List<int> row in grid) {
113+
int ub = row.reversed.toList().lastIndexWhere((element) => element < 0);
114+
count += ub + 1;
115+
}
116+
117+
return count;
118+
}
119+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import "sort"
4+
5+
func countNegatives1(grid [][]int) int {
6+
var count int = 0
7+
var rows int = len(grid)
8+
var columns int = len(grid[0])
9+
var row int = rows - 1
10+
var column int = 0
11+
for column < columns && row >= 0 {
12+
if grid[row][column] < 0 {
13+
count += columns - column
14+
row--
15+
} else {
16+
column++
17+
}
18+
}
19+
return count
20+
}
21+
22+
func countNegatives(grid [][]int) int {
23+
var gridRowsLength int = len(grid)
24+
var count int = 0
25+
26+
for i := 0; i < gridRowsLength; i++ {
27+
sort.Sort(sort.Reverse(sort.IntSlice(grid[i])))
28+
upperBound := sort.Search(len(grid[i]), func(j int) bool {
29+
return grid[i][j] < 0
30+
})
31+
count += len(grid[i]) - upperBound
32+
}
33+
34+
return count
35+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# A
2+
3+
To count negative numbers in a sorted matrix, you can use a simple algorithm that takes advantage of the sorted property of the matrix. Here's a step-by-step approach to count negative numbers efficiently:
4+
5+
1. Initialize a count variable to 0, which will store the number of negative numbers encountered.
6+
2. Start from the top-right corner of the matrix.
7+
3. Compare the current element with 0:
8+
- If the current element is less than 0, it means the entire column to the left of the current element will also be negative. Increment the count variable by the number of elements in that column (including the current element).
9+
- If the current element is greater than or equal to 0, move to the next row (go down).
10+
4. Repeat step 3 until you reach the bottom-left corner of the matrix.
11+
5. Return the count variable, which will contain the total count of negative numbers in the matrix.
12+
13+
Here's a Python code implementation of the algorithm:
14+
15+
```dart
16+
17+
```
18+
19+
You can use this function by passing your sorted matrix as an argument. It will return the count of negative numbers in the matrix.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
224224
- [**405.** Convert a Number to Hexadecimal](ConvertANumberToHexadecimal/convert_a_number_to_hexadecimal.dart)
225225
- [**1502.** Can Make Arithmetic Progression From Sequence](CanMakeArithmeticProgressionFromSequence/can_make_arithmetic_progression_from_sequence.dart)
226226
- [**409.** Longest Palindrome](LongestPalindrome/longest_palindrome.dart)
227+
- [**1351.** Count Negative Numbers in a Sorted Matrix](CountNegativeNumbersInASortedMatrix/count_negative_numbers_in_a_sorted_matrix.dart)
227228

228229
## Reach me via
229230

0 commit comments

Comments
 (0)