-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1351.php
42 lines (34 loc) · 834 Bytes
/
1351.php
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
<?php
class Solution
{
/**
* @param Integer[][] $grid
* @return Integer
*/
function countNegatives($grid)
{
$cnt = 0;
for ($i = 0; $i < count($grid); $i++) {
$len = count($grid[$i]);
$cnt += ($len - ($this->binarySearch($grid[$i], $len - 1) + 1));
}
return $cnt;
}
function binarySearch(array $arr, $end)
{
$start = 0;
$ans = -1;
while ($start <= $end) {
$mid = (int)(($start + $end) / 2);
if ($arr[$mid] < 0) {
$end = $mid - 1;
$ans = $mid - 1;
} else {
$start = $mid + 1;
$ans = $mid;
}
}
return $ans;
}
}
//[[3,-1,-3,-3,-3],[2,-2,-3,-3,-3],[1,-2,-3,-3,-3],[0,-3,-3,-3,-3]]