left
and right
of the same length as seats
, filled with zeros.seats
from left to right:
seats[i]
is 1
, set left[i]
to 0
.left[i]
to left[i-1] + 1
, with left[0]
set to a large number if seats[0]
is 0
.seats
from right to left:
seats[i]
is 1
, set right[i]
to 0
.right[i]
to right[i+1] + 1
, with right[-1]
set to a large number if seats[-1]
is 0
.i
and for each empty seat, calculate min(left[i], right[i])
.