Skip to content

Commit 2ee0889

Browse files
authored
Create leftmost-column-with-at-least-a-one.cpp
1 parent 6883d36 commit 2ee0889

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(m + n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) {
7+
const auto& dim = binaryMatrix.dimensions();
8+
const auto& m = dim[0], &n = dim[1];
9+
int r = 0, c = n - 1;
10+
while (r < m && c >= 0) {
11+
if (!binaryMatrix.get(r, c)) {
12+
++r;
13+
} else {
14+
--c;
15+
}
16+
}
17+
return (c + 1 != n) ? c + 1 : -1;
18+
}
19+
};

0 commit comments

Comments
 (0)