Given a rows x cols
binary matrix mat
, find the leftmost column (0-indexed) with at least one 1
in it.
Return the index of the leftmost column with at least one 1
in it. If such a column does not exist, return -1
.
You must solve the problem in O(rows + cols)
or better.
Example 1:
Input: mat = [[0,0,0,1],[0,0,1,1],[0,1,1,1]] Output: 1
Example 2:
Input: mat = [[0,0,0,0],[0,0,0,0],[0,0,0,0]] Output: -1
Example 3:
Input: mat = [[0,0,0,1],[0,0,1,1],[0,1,1,1]] Output: 1
Constraints:
rows == mat.length
cols == mat[i].length
1 <= rows, cols <= 100
mat[i][j]
is either 0
or 1
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach is very simple: We examine each column from left to right. For each column, we check every single cell in that column to see if it contains a one.
Here's how the algorithm would work step-by-step:
def leftmost_column_with_one_brute_force(binary_matrix):
number_of_rows = len(binary_matrix)
number_of_columns = len(binary_matrix[0]) if number_of_rows > 0 else 0
# Iterate through each column from left to right
for column_index in range(number_of_columns):
# Check each row in the current column for a one
for row_index in range(number_of_rows):
# If a one is found, return the current column index
if binary_matrix[row_index][column_index] == 1:
return column_index
# No column with a one was found
return -1
We're looking for the leftmost column that has a '1' in it. The key is to start in a strategic spot and move in a way that eliminates large sections of the data with each step. This avoids checking every single cell in the table.
Here's how the algorithm would work step-by-step:
def find_leftmost_column_with_one(binary_matrix):
rows = len(binary_matrix)
cols = len(binary_matrix[0]) if rows > 0 else 0
leftmost_column = -1
row_index = 0
column_index = cols - 1
while row_index < rows and column_index >= 0:
if binary_matrix[row_index][column_index] == 1:
# Found a '1', so update leftmost and move up.
leftmost_column = column_index
row_index += 1
else:
# Current is '0', move to the left.
column_index -= 1
return leftmost_column
Case | How to Handle |
---|---|
Null matrix input | Return -1 immediately as a null matrix does not have any columns. |
Empty matrix (0 rows) | Return -1 since there are no columns to check if the matrix has 0 rows. |
Matrix with 0 columns | Return -1 because if a matrix has zero columns it has no leftmost column containing a one. |
Matrix with all zeros | Return -1 since there will be no leftmost column with at least a one. |
Matrix where the leftmost column containing a one is the first column (index 0) | The algorithm should correctly identify and return 0. |
Matrix where all rows have a 1 in the first column | The binary search should still function correctly, efficiently finding index 0. |
Very large matrix (potential for integer overflow if calculating column indices naively) | Use appropriate data types and/or avoid explicit multiplication when possible to prevent overflow. |
Matrix where all rows have a '1' but '1' appears in different columns for each row. | The algorithm should binary search each row to find the leftmost '1' and keep track of the minimum column index. |