left = 1
and right = n
(total number of days).left < right
, perform the following steps:
a. Calculate the middle day mid
as left + (right - left) / 2
.
b. Perform BFS to check if a path exists from the top to the bottom on day mid
.
c. If a path exists, set left = mid + 1
, otherwise set right = mid
.left - 1
will be the last day when it is possible to walk from top to bottom.