Corridor Run With Matching Keys and Locked Doors
StringsAuto-graded · 27 test casesLast updated
Find the fewest moves along a 1D corridor with walls, keys and doors, and notice that the state space collapses to a simple prefix scan.
Corridor Run With Matching Keys and Locked Doors
A corridor is described by a string corridor of length n. Each character is one cell:
'.'is an open cell.'#'is a wall and can never be entered.- A lowercase letter
'a'-'z'is a key. Entering the cell picks it up, and you keep it for the rest of the trip. - An uppercase letter
'A'-'Z'is a locked door. You may enter it only if you already hold the key with the same letter in lowercase (door'C'needs key'c'). Doors are not consumed, and keys are not used up.
You begin at index 0. In one move you step to the cell directly to the left or right, and each move costs 1 unit of time. You may revisit cells and move in either direction.
Some rules for the starting cell:
- You are already standing on index
0, so whatever it contains does not block you at the start. - If index
0holds a key, you pick it up immediately.
Return the minimum time needed to stand on index n - 1. Return 0 if n == 1, and return -1 if the last cell cannot be reached.
The last cell follows the same entry rules as any other cell. If it is a wall, or a door whose key you cannot hold, the answer is -1.
Constraints
1 <= n <= 2000corridorcontains only'.','#', and letters'a'-'z'/'A'-'Z'.- The same key letter may appear several times. Holding one copy is the same as holding several.
- A door may have no matching key anywhere in the corridor.
Examples
Example 1
Input: min_time_to_exit('.a.A.b.B.')
Output: 8
Example 2
Input: min_time_to_exit('.A.a.')
Output: -1
Hints
Hint 1
Think of your state as (position, set of keys held). Can you write a BFS over it? Then ask how many different key sets can actually occur at a given position.
Hint 2
To reach the last cell you must step on every cell between the start and it. What does that imply about which keys you hold when you first arrive at index i?
Hint 3
If walking right without ever turning back is allowed by the rules, it is also optimal. So the answer is either n - 1 or -1, and a single left-to-right scan that tracks collected keys decides which.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The general technique for key-and-door mazes is a BFS over states (position, keys held). In a one-dimensional corridor that search collapses to a simple scan.
Key observation. To reach index n - 1 from index 0, you must step on every index in between. So when you first arrive at index i, you have visited exactly the cells 0..i, and you hold exactly the keys located in 0..i. Walking backwards never gives you a key you have not already collected, so backtracking cannot help.
This leaves two possible answers:
n - 1if walking straight to the right is legal.-1otherwise.
Walking straight is legal when, for every index i from 1 to n - 1:
corridor[i]is not a wall, and- if
corridor[i]is a door, its matching key appeared at some index beforei.
The cell at index 0 is never treated as an obstacle, but a key there is collected.
Algorithm
- If
n == 1, return0. - Keep a bitmask of keys collected so far.
- Scan the cells left to right. For
i > 0, return-1on a wall, or on a door whose key bit is not set. - If the cell is a key, set its bit.
- If the scan finishes, return
n - 1.
Why it is correct
- Feasibility does not depend on the path. Every route to index
ihas visited all of0..i, so the keys held at the moment of first reachingiare exactly those in the prefix. A door atiis passable if and only if its key lies earlier in the string. - Impossible case. If some wall or door at index
ifails the check, every route must crossi. No route can hold the needed key at that point, so the answer is-1. - Optimal case. The distance from
0ton - 1is at leastn - 1. The straight walk achieves it when it is legal.
This is the same answer a full BFS over (index, mask) would return, because only one mask is ever reachable per index.
Complexity
- Time:
O(n). - Space:
O(1), since the bitmask has at most 26 bits.
Reference solution
def min_time_to_exit(corridor: str) -> int:
n = len(corridor)
if n == 1:
return 0
keys = 0 # bitmask of collected keys
for i, c in enumerate(corridor):
if i > 0:
if c == '#':
return -1
if 'A' <= c <= 'Z':
if not (keys & (1 << (ord(c) - ord('A')))):
return -1
if 'a' <= c <= 'z':
keys |= 1 << (ord(c) - ord('a'))
return n - 1