Count Negative Numbers in a Sorted Matrix - LeetCode
0 is not negative, so it goes in the same branch as positivesint countNegatives(vector<vector<int>>& grid) {
int negatives = 0;
for (auto& row : grid) {
int left = 0, right = row.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (row[mid] < 0) {
right = mid - 1;
} else { // >= 0, including 0
left = mid + 1;
}
}
negatives += row.size() - left;
}
return negatives;
}
left lands on the first negative index, row.size() - left is the countright = row.size() instead of row.size() - 1, out of bounds on the first row[mid]row[mid] <= 0 to push right left, which counts 0 as negativeO(n + m), this is O(m log n)O(n + m), not done yet