-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryMatrix.java
61 lines (54 loc) · 1.85 KB
/
BinaryMatrix.java
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
package techQuestions;
import java.util.ArrayList;
import java.util.List;
interface BinaryMatrix {
public int get(int x, int y);
public List<Integer> dimensions();
};
class Solution implements BinaryMatrix {
int grid[][] = {{0,0,0,1},{0,0,1,1},{0,1,1,1}};
public int get(int x, int y) {
return grid[x][y];
}
public List<Integer> dimensions() {
List<Integer> dimensions = new ArrayList<>();
dimensions.add(grid.length);
dimensions.add(grid[0].length);
return dimensions;
}
public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) {
// System.out.println(dimensions().toString());
// System.out.println(dimensions().get(0));
// initialize a pointer p that starts at top-right corner
int p = binaryMatrix.dimensions().get(1) - 1; // far right column
int i = 0; // top row
int leftMostCol = Integer.MAX_VALUE;
while (p >= 0) {
// System.out.println("i is: " + i + ", p is: " + p);
// System.out.println("value here is: " + binaryMatrix.get(i, p));
if (binaryMatrix.get(i,p) == 0) {
// move down to next row if we find a 0
i++;
if (i == binaryMatrix.dimensions().get(0)) {
break;
}
continue;
} else {
// we found a one! update the leftMostColumn
leftMostCol = Math.min(p, leftMostCol);
p--;
}
}
// if we never found a 0, leftMost will still be huge
if (leftMostCol == Integer.MAX_VALUE) {
return -1;
}
return leftMostCol;
}
}
class MyMainClass {
public static void main(String[] args) {
Solution myMat = new Solution();
System.out.println(myMat.leftMostColumnWithOne(myMat));
}
}