(This problem is an interactive problem.)
A binary matrix means that all elements are 0
or 1
. For each individual row of the matrix, this row is sorted in non-decreasing order.
Given a row-sorted binary matrix binaryMatrix, return leftmost column index(0-indexed) with at least a 1
in it. If such index doesn't 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 a list of 2 elements[rows, cols]
, which means the matrix isrows * 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 you're given the binary matrix mat
as input in the following four examples. You will not have access the binary matrix directly.
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.
至少有一個 1 的最左端列。
題目大意是給一個二維矩陣 matrix,里面只有 0 和 1 兩種數字,matrix 的每一行的數字都是非遞減的(但是每一列並不是非遞減)。你不能直接訪問這個 matrix,但是你可以通過給的接口訪問 matrix 的一些東西,比如 dimensions() 可以拿到 matrix 的 dimension 尺寸,get(x, y) 可以拿到 某一個坐標上的值。請你返回這個矩陣里面最左邊的包含起碼一個 1 的 column 的 index。若這個 column 不存在則返回 -1。
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
.
幾個例子如下,
Example 1:
Input: mat = [[0,0],[1,1]] Output: 0Example 2:
Input: mat = [[0,0],[0,1]] Output: 1Example 3:
Input: mat = [[0,0],[0,0]] Output: -1Example 4:
Input: mat = [[0,0,0,1],[0,0,1,1],[0,1,1,1]] Output: 1
我這里提供兩種思路,一種是逐行掃描,一種是二分法。
首先是逐行掃描,先用 dimensions() 函數得到 matrix 的長和寬,然后從 matrix 的右下角的位置(m - 1, n - 1)開始看是否能找到一個 1。若 cur 為 0,則再往上一行找(i--);若 cur 為 1,將當前坐標的縱坐標標記為 res,再往左邊看是否還有縱坐標更小的 1。
時間O(m + n) - 長 + 寬
空間O(1)
Java實現
1 /** 2 * // This is the BinaryMatrix's API interface. 3 * // You should not implement it, or speculate about its implementation 4 * interface BinaryMatrix { 5 * public int get(int row, int col) {} 6 * public List<Integer> dimensions {} 7 * }; 8 */ 9 10 class Solution { 11 public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { 12 List<Integer> dimension = binaryMatrix.dimensions(); 13 int n = dimension.get(0); 14 int m = dimension.get(1); 15 int i = n - 1, j = m - 1, res = -1; 16 while (i >= 0 && j >= 0) { 17 int cur = binaryMatrix.get(i, j); 18 if (cur == 0) { 19 i--; 20 } else { 21 res = j; 22 j--; 23 } 24 } 25 return res; 26 } 27 }
二分法的思路也比較直觀。因為每一行是非遞減的,所以是在對每一行做二分。我們一開始先試圖對最后一行做二分,得到最后一行的 mid 坐標之后,我們看在最后一行的 mid 位置上是否存在 1,如果存在,則把 mid 暫時記錄成 res,然后可以在 left - mid 的范圍內再次對每一行掃描,看看是否在其他行存在一個 index 比 mid 更小的 1。
時間O(mlogn)
空間O(1)
Java實現
1 /** 2 * // This is the BinaryMatrix's API interface. 3 * // You should not implement it, or speculate about its implementation 4 * interface BinaryMatrix { 5 * public int get(int row, int col) {} 6 * public List<Integer> dimensions {} 7 * }; 8 */ 9 10 class Solution { 11 public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { 12 List<Integer> di = binaryMatrix.dimensions(); 13 int m = di.get(0); 14 int n = di.get(1); 15 int left = 0; 16 int right = n - 1; 17 int res = -1; 18 while (left <= right) { 19 // 某一行的中點mid 20 int mid = left + (right - left) / 2; 21 // 逐行掃描,看當前行是否在mid處是1 22 if (helper(binaryMatrix, m, mid)) { 23 res = mid; 24 right = mid - 1; 25 } else { 26 left = mid + 1; 27 } 28 } 29 return res; 30 } 31 32 private boolean helper(BinaryMatrix binaryMatrix, int m, int c) { 33 for (int r = 0; r < m; r++) { 34 if (binaryMatrix.get(r, c) == 1) { 35 return true; 36 } 37 } 38 return false; 39 } 40 }