1428. Leftmost Column with at Least a One
Previous1423. Maximum Points You Can Obtain from CardsNext1461. Check If a String Contains All Binary Codes of Size K
Last updated
Last updated
interface BinaryMatrix {
public int get(int row, int col) {}
public List<Integer> dimensions {}
};/**
* Time complexity :
* Space complexity :
*/
class Solution {
public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) {
if(binaryMatrix == null) {
return 0;
}
List<Integer> dimension = binaryMatrix.dimensions();
int rows = dimension.get(0), cols = dimension.get(1);
int minIndex = Integer.MAX_VALUE;
int low = 0, high = cols-1;
while(low <= high) {
int col = low + (high - low)/2;
boolean foundOne = false;
for(int row = 0; row < rows; row++) {
if(binaryMatrix.get(row, col) == 1) {
foundOne = true;
minIndex = Math.min(minIndex, col);
break;
}
}
if(foundOne) {
high = col - 1;
} else {
low = col + 1;
}
}
return minIndex == Integer.MAX_VALUE? -1: minIndex;
}
}