-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleftestColumnwithOnejs.js
More file actions
48 lines (44 loc) · 1.16 KB
/
leftestColumnwithOnejs.js
File metadata and controls
48 lines (44 loc) · 1.16 KB
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
/* // This is the BinaryMatrix's API interface.
* // You should not implement it, or speculate about its implementation
* function BinaryMatrix() {
* @param {integer} row, col
* @return {integer}
* this.get = function(row, col) {
* ...
* };
*
* @return {[integer, integer]}
* this.dimensions = function() {
* ...
* };
* };
*/
/* @param {BinaryMatrix} binaryMatrix
* @return {number}
*/
var leftMostColumnWithOne = function(binaryMatrix) {
let ans = +Infinity;
let left = 0;
let right = binaryMatrix.dimensions()[1] - 1;
let found = [...Array(binaryMatrix.dimensions()[0]).keys()];
return findTheOne(left, right, found);
function findTheOne(left, right, found){
if(right<left) return ans == +Infinity ? -1 : ans;
let temp = new Array;
let col = Math.floor((left + right)/2)
let i = 0;
while(i < found.length ){
if(binaryMatrix.get(found[i], col) == 1) temp.push(found[i]);
i++;
}
if(temp.length > 0 ) {
ans = col < ans ? col : ans;
right = col - 1;
found = temp;
}
else{
left = col + 1;
}
return findTheOne(left, right, found);
}
};