(This problem is an interactive problem.)
A binary matrix means that all elements are
Given a row-sorted binary matrix binaryMatrix, return leftmost column index(0-indexed) with at least a
You can’t access the Binary Matrix directly. You may only access the matrix using a
BinaryMatrix.get(row, col) returns the element of the matrix at index(row, col) (0-indexed).BinaryMatrix.dimensions() returns a list of 2 elements[rows, cols] , which means the matrix isrows * cols .
Submissions making more than
For custom testing purposes you’re given the binary matrix
Example 1:
Example 2:
Example 3:
Example 4:
Constraints:
rows == mat.length cols == mat[i].length 1 <= rows, cols <= 100 mat[i][j] is either0 or1 .mat[i] is sorted in a non-decreasing way.
* Images Source : LeetCode
Code
0 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 |
/** * // 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 {} * }; */ class Solution { public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { List<Integer> dimension=binaryMatrix.dimensions(); int n=dimension.get(0); int m=dimension.get(1); int i=0,j=m-1,leftMostOne=-1; while(i<n && j>=0) { int result=binaryMatrix.get(i,j); if(result==0) i++; else{ leftMostOne=j; j--; } } return leftMostOne; } } |
We encourage you to write a comment if you have a better solution or having any doubt on the above topic.