- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo_6.java
More file actions
Latest commit
40 lines (37 loc) · 1.09 KB
/
Copy pathDemo_6.java
File metadata and controls
40 lines (37 loc) · 1.09 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
classCountNegativeNumbersInSortedMatrix{
// public int countNegatives(int[][] grid) {
// int count = 0;
// for(int[] row: grid){
// for(int val: row){
// if(val<0){
// count++;
// }
// }
// }
// return count;
// }
publicintcountNegatives(int[][] grid) {
intcount = 0;
for(int[] row: grid){
intlow = 0;
inthigh = row.length-1;
while(low<=high){
intmid = (low + high)/2;
if(row[mid]<0){
high = mid - 1;
} else {
low = mid + 1;
}
}
count += (row.length - low);
}
returncount;
}
}
publicclassDemo_6 {
publicstaticvoidmain(String[] args) {
CountNegativeNumbersInSortedMatrixobj = newCountNegativeNumbersInSortedMatrix();
intres = obj.countNegatives(newint[][]{{4,3,2,-1},{3,2,1,-1},{1,1,-1,-2},{-1,-1,-2,-3}});
System.out.println(res);//8
}
}