Leftmost Column Containing a One in Row-Sorted Grid
Arrays & HashingAuto-graded · 21 test casesLast updated
Find the leftmost column holding a 1 in a binary grid whose rows are sorted, aiming for a linear-time walk instead of scanning every cell.
Leftmost Column Containing a One
You are given a grid of 0s and 1s with rows rows and cols columns. Within every row the values are sorted in non-decreasing order, so each row is a block of 0s followed by a block of 1s (either block may be empty).
Return the index (0-based) of the leftmost column that contains at least one 1. If the grid contains no 1 at all, return -1.
In the original setting each cell lookup is considered costly, so a good solution reads as few cells as possible. Aim for a solution that reads at most about rows + cols cells rather than inspecting all rows * cols of them.
Function
Implement leftmost_one_column(rows, cols, grid):
rows: the number of rows in the grid.cols: the number of columns in the grid.grid: a list ofrowslists, each withcolsintegers (0or1), every row sorted in non-decreasing order.
Return a single integer: the smallest column index containing a 1, or -1 if there is none.
Constraints
1 <= rows, cols <= 10^5- Every entry of
gridis0or1. - Each row of
gridis sorted in non-decreasing order. - Treat each access
grid[r][c]as an expensive read and try to keep the number of reads low.
Edge cases to handle
- A grid made entirely of
0s returns-1. - A grid made entirely of
1s returns0. - Grids with a single row or a single column.
Examples
Example 1
Input: leftmost_one_column(3, 4, [[0, 0, 1, 1], [0, 0, 0, 1], [0, 1, 1, 1]])
Output: 1
Example 2
Input: leftmost_one_column(2, 3, [[0, 0, 0], [0, 0, 0]])
Output: -1
Hints
Hint 1
Because each row is sorted, a 0 at some position means everything to its left in that row is also 0.
Hint 2
Start at the top-right cell and keep a column pointer that never moves right. What should you do on a 1, and what on a 0?
Hint 3
On a 1, remember the column and step left; on a 0, drop to the next row without changing the column. Each read either moves the pointer left or finishes a row, so at most rows + cols reads happen.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Use a "staircase" walk. Place a column pointer col at the last column and process rows from top to bottom. The pointer only ever moves left.
- In the current row, while
grid[r][col]is1, recordcolas the best answer so far and move the pointer one step left. There might be an even earlier1in this row. - When the cell is
0, this row cannot contribute anything further to the left ofcol, because the row is sorted and everything at or beforecolis0. Move on to the next row, keepingcolunchanged. - If the pointer falls below column 0, a
1sits in column 0, which cannot be beaten, so return0. - After all rows are processed, return the recorded best column (
-1if no1was ever seen).
Why it is correct
After finishing a row, best is the leftmost column holding a 1 among the rows handled so far, and col equals best - 1 (or cols - 1 if nothing has been found).
- Stepping left on a
1is safe because we store the column before moving. - Dropping to the next row on a
0is safe because the sortedness of the row means no1exists at or to the left ofcolin that row. - Never moving right is safe: a later row's
1located at or to the right of the current best column cannot improve the answer.
If every row ends with a 0 in the last column, then, as rows are sorted, the whole grid is zeros and -1 is correct.
Complexity
- Time: O(rows + cols) reads. Each read either shifts the pointer left (at most
colstimes overall) or ends a row (at mostrowstimes). - Space: O(1).
Reference solution
from typing import List
def leftmost_one_column(rows: int, cols: int, grid: List[List[int]]) -> int:
col = cols - 1
best = -1
for r in range(rows):
while col >= 0 and grid[r][col] == 1:
best = col
col -= 1
if col < 0:
return 0
return best