← 返回 uber 的题目列表Leftmost Column with at Least a One
类型:online_judge
Problem: Leftmost Column with at Least a One
You are given an m x n binary matrix mat. Each row is sorted in non-decreasing order, meaning each row consists of some number of 0s followed by some number of 1s.
Return the index of the leftmost column that contains at least one 1. If no such column exists, return -1.
Column indices are 0-based.
Input Format
For testing purposes, the input is provided as a regular matrix:
m n
mat[0][0] mat[0][1] ... mat[0][n-1]
...
mat[m-1][0] mat[m-1][1] ... mat[m-1][n-1]
Output Format
Print one integer: the leftmost column index containing at least one 1, or -1 if there is no 1 in the matrix.
Example 1
Input:
3 4
0 0 0 1
0 0 1 1
0 1 1 1
Output:
1
Example 2
Input:
2 3
0 0 0
0 0 0
Output:
-1
Constraints
1 <= m, n <= 1000
mat[i][j] is either 0 or 1
Each row is sorted in non-decreasing order
LeetCode Original Note
In LeetCode 1428, the matrix is accessed through a BinaryMatrix API:
BinaryMatrix.get(row, col) returns the value at that position
BinaryMatrix.dimensions() returns [m, n]
Interviewers usually expect a solution that minimizes the number of get calls.
Example
Input
3 4
0 0 0 1
0 0 1 1
0 1 1 1
Output
1