A row-sorted binary matrix means that all elements are 0 or 1 and each row of the matrix is sorted in non-decreasing order.
Given a row-sorted binary matrixbinaryMatrix, return the index (0-indexed) of the leftmost column with a 1 in it. If such an index does not exist, return -1.
You can't access the Binary Matrix directly. You may only access the matrix using a BinaryMatrix interface:
BinaryMatrix.get(row, col) returns the element of the matrix at index (row, col) (0-indexed).
BinaryMatrix.dimensions() returns the dimensions of the matrix as a list of 2 elements [rows, cols], which means the matrix is rows x cols.
Submissions making more than 1000 calls to BinaryMatrix.get will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.
For custom testing purposes, the input will be the entire binary matrix mat. You will not have access to the binary matrix directly.
Example 1:
Input: mat = [[0,0],[1,1]]
Output: 0
Example 2:
Input: mat = [[0,0],[0,1]]
Output: 1
Example 3:
Input: mat = [[0,0],[0,0]]
Output: -1
Constraints:
rows == mat.length
cols == mat[i].length
1 <= rows, cols <= 100
mat[i][j] is either 0 or 1.
mat[i] is sorted in non-decreasing order.
Solutions
Solution 1: Binary Search
First, we call BinaryMatrix.dimensions() to get the number of rows $m$ and columns $n$ of the matrix. Then for each row, we use binary search to find the column number $j$ where the leftmost $1$ is located. The smallest $j$ value that satisfies all rows is the answer. If there is no such column, return $-1$.
The time complexity is $O(m \times \log n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. We need to traverse each row, and use binary search within each row, which has a time complexity of $O(\log n)$. The space complexity is $O(1)$.
1 2 3 4 5 6 7 8 91011121314151617
# """# This is BinaryMatrix's API interface.# You should not implement it, or speculate about its implementation# """# class BinaryMatrix(object):# def get(self, row: int, col: int) -> int:# def dimensions(self) -> list[]:classSolution:defleftMostColumnWithOne(self,binaryMatrix:"BinaryMatrix")->int:m,n=binaryMatrix.dimensions()ans=nforiinrange(m):j=bisect_left(range(n),1,key=lambdak:binaryMatrix.get(i,k))ans=min(ans,j)return-1ifans>=nelseans
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * interface BinaryMatrix { * public int get(int row, int col) {} * public List<Integer> dimensions {} * }; */classSolution{publicintleftMostColumnWithOne(BinaryMatrixbinaryMatrix){List<Integer>e=binaryMatrix.dimensions();intm=e.get(0),n=e.get(1);intans=n;for(inti=0;i<m;++i){intl=0,r=n;while(l<r){intmid=(l+r)>>1;if(binaryMatrix.get(i,mid)==1){r=mid;}else{l=mid+1;}}ans=Math.min(ans,l);}returnans>=n?-1:ans;}}
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * class BinaryMatrix { * public: * int get(int row, int col); * vector<int> dimensions(); * }; */classSolution{public:intleftMostColumnWithOne(BinaryMatrix&binaryMatrix){autoe=binaryMatrix.dimensions();intm=e[0],n=e[1];intans=n;for(inti=0;i<m;++i){intl=0,r=n;while(l<r){intmid=(l+r)>>1;if(binaryMatrix.get(i,mid)){r=mid;}else{l=mid+1;}}ans=min(ans,l);}returnans>=n?-1:ans;}};
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * type BinaryMatrix struct { * Get func(int, int) int * Dimensions func() []int * } */funcleftMostColumnWithOne(binaryMatrixBinaryMatrix)int{e:=binaryMatrix.Dimensions()m,n:=e[0],e[1]ans:=nfori:=0;i<m;i++{l,r:=0,nforl<r{mid:=(l+r)>>1ifbinaryMatrix.Get(i,mid)==1{r=mid}else{l=mid+1}}ans=min(ans,l)}ifans>=n{return-1}returnans}
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * class BinaryMatrix { * get(row: number, col: number): number {} * * dimensions(): number[] {} * } */functionleftMostColumnWithOne(binaryMatrix:BinaryMatrix){const[m,n]=binaryMatrix.dimensions();letans=n;for(leti=0;i<m;++i){let[l,r]=[0,n];while(l<r){constmid=(l+r)>>1;if(binaryMatrix.get(i,mid)===1){r=mid;}else{l=mid+1;}}ans=Math.min(ans,l);}returnans>=n?-1:ans;}
/** * // This is the BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * struct BinaryMatrix; * impl BinaryMatrix { * fn get(row: i32, col: i32) -> i32; * fn dimensions() -> Vec<i32>; * }; */implSolution{pubfnleft_most_column_with_one(binaryMatrix:&BinaryMatrix)->i32{lete=binaryMatrix.dimensions();letm=e[0]asusize;letn=e[1]asusize;letmutans=n;foriin0..m{let(mutl,mutr)=(0,n);whilel<r{letmid=(l+r)/2;ifbinaryMatrix.get(iasi32,midasi32)==1{r=mid;}else{l=mid+1;}}ans=ans.min(l);}ifans>=n{-1}else{ansasi32}}}
/** * // This is BinaryMatrix's API interface. * // You should not implement it, or speculate about its implementation * class BinaryMatrix { * public int Get(int row, int col) {} * public IList<int> Dimensions() {} * } */classSolution{publicintLeftMostColumnWithOne(BinaryMatrixbinaryMatrix){vare=binaryMatrix.Dimensions();intm=e[0],n=e[1];intans=n;for(inti=0;i<m;++i){intl=0,r=n;while(l<r){intmid=(l+r)>>1;if(binaryMatrix.Get(i,mid)==1){r=mid;}else{l=mid+1;}}ans=Math.Min(ans,l);}returnans>=n?-1:ans;}}