A wave through the audience
Write code · Challenge · §9.3 Nearest first
The seats of a live show recorded with a studio audience are given as the grid seats. 'O' is a seat where an audience member is sitting, '.' is an empty seat, and 'W' is the seat of an audience member who is already standing at the start to begin the wave.
Every second, all sitting audience members in the seats directly above, below, left of, or right of a standing audience member stand up together. Once an audience member stands up, they stay standing. An empty seat cannot pass the wave on. Complete the function solution, which returns how many seconds it takes until every audience member is standing. If nobody is sitting at the start, return 0. If some audience member never stands up, return -1.
Function
solution(seats)
Constraints
- The length of
seatsis from 1 to 200. - The strings in
seatsall have the same length, which is from 1 to 200. - The strings in
seatsconsist only of'O','.', and'W', and there is at least one'W'.
Examples
| seats | result |
|---|---|
['OOO', 'O.O', 'OOW'] | 4 |
['OOO.OO', 'OOW.OW'] | 3 |
Starter code
def solution(seats):
answer = 0
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.