-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount-negatives.js
93 lines (61 loc) · 2.72 KB
/
count-negatives.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
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
*/
// ---------------------------------------------------------------------------------------------------------------------------
// This is linear search
const countNegatives2 = (grid) => {
negatives = [];
for(let i in grid) {
for(j in grid[ i ]) {
if(grid[ i ][ j ] < 0) {
negatives.push(j);
}
}
}
console.log(negatives);
return negatives.length;
};
// const answer2 = countNegatives2([ [ 4, 3, 2, -1 ], [ 3, 2, 1, -1 ], [ 1, 1, -1, -2 ], [ -1, -1, -2, -3 ] ]);
// const answer = countNegatives2([3,2],[1,0])
// console.log(answer2)
// ---------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------
/* using Binary Search algorithm*/
const countNegatives = (grid) => {
let count = 0;
for(let subArr of grid) {
let start = 0;
let end = subArr.length - 1;
while(start <= end) {
let middle = Math.floor(start + (end - start) / 2);
console.log(middle)
if(subArr[ middle ] < 0) {
count = count + end - middle + 1;
end = middle - 1;
} else {
start = middle + 1;
}
}
}
return count;
};
const answer = countNegatives([ [ 4, 3, 2, -1 ], [ 3, 2, 1, -1 ], [ 1, 1, -1, -2 ], [ -1, -1, -2, -3 ]]);
console.log(answer)
// ---------------------------------------------------------------------------------------------------------------------------
/*
1. Check if the value is less than 0
If so, we have found a negative number. We update our counter by adding right - mid + 1
as all elements from mid to right will also be negative
Update right pointer to mid - 1 to search for a negative number in the left half of the row
2. If the value is greater than or equal to 0, this means the negative number are located in the right half of the row
In this case, we update left pointer to mid + 1 to search in the right half of the row
3. Repeat this process until, left pointer becomes greater than the right pointer indicating,
we have found all the negative numbers or that there are no negatives numbers in the row
*/