Maximum Straight-Line Distance Through Dangerous Zones
Arrays & HashingAuto-graded · 20 test casesLast updated
Tests efficient grid reasoning with blocked regions and directional movement constraints using binary search.
You have a rectangular grid with num_rows rows and num_cols columns. Certain positions contain hazards that make entire rows and columns impassable. Specifically, a hazard located at position (r, c) renders every cell in row r unsafe and every cell in column c unsafe.
A vehicle begins at position (start_row, start_col). While the vehicle's initial position remains safe regardless of hazards, it must travel in a single straight line (up, down, left, or right) without changing direction. The vehicle can only move through cells that are:
- Within grid boundaries
- Not made unsafe by any hazard
Note that the safety of the starting position does not extend to any other cell, including the very first cell the vehicle enters.
Determine the maximum number of cells the vehicle can pass through when moving optimally in one of the four cardinal directions. The starting cell itself does not count toward this total. If no safe movement exists in any direction, return 0.
Coordinates: All positions use 1-based indexing. Rows range from 1 to num_rows, and columns range from 1 to num_cols.
Constraints:
1 ≤ num_rows, num_cols ≤ 10^91 ≤ start_row ≤ num_rows1 ≤ start_col ≤ num_cols0 ≤ len(hazard_positions) ≤ 10^5- Each hazard position
[r, c]satisfies1 ≤ r ≤ num_rowsand1 ≤ c ≤ num_cols
Examples
Example 1
Input: max_straight_distance(8, 8, 5, 3, [[1, 6], [2, 8]])
Output: 3
Example 2
Input: max_straight_distance(5, 5, 3, 3, [[1, 1], [5, 5]])
Output: 1
Example 3
Input: max_straight_distance(10, 12, 5, 6, [])
Output: 6
Hints
Hint 1
A hazard blocks an entire row and an entire column. You don't need to simulate individual cells.
Hint 2
Moving vertically keeps you in the same column. If that column is blocked, vertical movement is impossible.
Hint 3
Use binary search to find the nearest blocked row or column in each direction.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each hazard creates two blocked lines: one entire row and one entire column. Any cell lying on a blocked row or blocked column becomes unsafe. The key insight is that we don't need to examine individual cells—we only need to track which rows and columns contain hazards.
When moving in a straight line:
- Vertical movement (up or down): The vehicle stays in
start_col. If that column contains a hazard, all cells in it are unsafe and vertical movement is impossible. Otherwise, the vehicle can move until it encounters a blocked row or reaches the grid boundary. - Horizontal movement (left or right): The vehicle stays in
start_row. If that row contains a hazard, horizontal movement is impossible. Otherwise, the vehicle can move until it encounters a blocked column or reaches the grid boundary.
For each valid direction, we use binary search to locate the nearest blocked line:
- Moving up: Find the closest blocked row below
start_row. The distance isstart_row - 1if none exists, otherwisestart_row - nearest_blocked_row - 1. - Moving down: Find the closest blocked row above
start_row. The distance isnum_rows - start_rowif none exists, otherwisenearest_blocked_row - start_row - 1. - Moving left: Find the closest blocked column to the left of
start_col. - Moving right: Find the closest blocked column to the right of
start_col.
The answer is the maximum distance among all four directions.
Why It Works
Since hazards block entire lines, a cell (r, c) is unsafe if and only if row r or column c contains a hazard. When traveling in a single direction along a clean line (a row or column without hazards), the vehicle can continue until hitting a perpendicular blocked line or the grid edge. Using binary search on sorted lists of blocked rows and columns efficiently finds these boundaries.
The starting position's safety exception only applies to that specific cell—it doesn't protect any destination cells, so we correctly check for blockers from the first step onward.
Complexity
- Time: O(H log H) where H is the number of hazards. We sort the blocked rows and columns, then perform constant binary searches.
- Space: O(H) to store the sets of blocked rows and columns.
Note that the grid dimensions don't affect complexity, making this solution efficient even for very large grids with sparse hazards.
Solution
from bisect import bisect_left
from typing import List
def max_straight_distance(num_rows, num_cols, start_row, start_col, hazard_positions):
blocked_rows = sorted({row for row, _ in hazard_positions})
blocked_cols = sorted({col for _, col in hazard_positions})
best = 0
# Vertical movement is impossible if the current column is destroyed.
if start_col not in blocked_cols:
pos = bisect_left(blocked_rows, start_row)
up = start_row - 1
if pos > 0:
up = min(up, start_row - blocked_rows[pos - 1] - 1)
down = num_rows - start_row
if pos < len(blocked_rows):
down = min(down, blocked_rows[pos] - start_row - 1)
best = max(best, up, down)
# Horizontal movement is impossible if the current row is destroyed.
if start_row not in blocked_rows:
pos = bisect_left(blocked_cols, start_col)
left = start_col - 1
if pos > 0:
left = min(left, start_col - blocked_cols[pos - 1] - 1)
right = num_cols - start_col
if pos < len(blocked_cols):
right = min(right, blocked_cols[pos] - start_col - 1)
best = max(best, left, right)
return best