-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1351-CountNegativeNumbersInASortedMatrix.go
More file actions
76 lines (67 loc) · 1.87 KB
/
1351-CountNegativeNumbersInASortedMatrix.go
File metadata and controls
76 lines (67 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
// 1351. Count Negative Numbers in a Sorted Matrix
// 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.
// Example 1:
// Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
// Output: 8
// Explanation: There are 8 negatives number in the matrix.
// Example 2:
// Input: grid = [[3,2],[1,0]]
// Output: 0
// Constraints:
// m == grid.length
// n == grid[i].length
// 1 <= m, n <= 100
// -100 <= grid[i][j] <= 100
import "fmt"
// O(n * n)
func countNegatives(grid [][]int) int {
res := 0
for i:= 0; i < len(grid); i++ {
for j:= 0; j < len(grid[i]); j++ {
if grid[i][j] < 0 {
res++
}
}
}
return res
}
// 二分
func countNegatives1(grid [][]int) int {
n,m := len(grid), len(grid[0])
l, r := 0 , m - 1
for l <= r {
mid := l + ((r - l) >> 1)
if grid[0][mid] >= 0 {
l = mid + 1
} else {
r = mid - 1
}
}
res := m - l
for i := 1; i < n; i++ {
for r > 0 && grid[i][r] < 0 {
r--
}
if grid[i][r] >= 0 {
res += m - r - 1
} else {
res += m - r
}
}
return res
}
func main() {
// Example 1:
// Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
// Output: 8
// Explanation: There are 8 negatives number in the matrix.
fmt.Println(countNegatives([][]int{ {4,3,2,-1}, {3,2,1,-1}, {1,1,-1,-2}, {-1,-1,-2,-3} })) // 8
// Example 2:
// Input: grid = [[3,2],[1,0]]
// Output: 0
fmt.Println(countNegatives([][]int{ {3,2}, {1,0}})) // 0
fmt.Println(countNegatives1([][]int{ {4,3,2,-1}, {3,2,1,-1}, {1,1,-1,-2}, {-1,-1,-2,-3} })) // 8
fmt.Println(countNegatives1([][]int{ {3,2}, {1,0}})) // 0
}