Skip to the code
cf.
+25 XP

#427 Minutes Until the Outage Spreads

medium

A status page simulates an outage spreading through a rack of servers drawn as a grid of strings: "X" is a server that is down, "o" is a healthy server, and "." is an empty slot. Every minute, each healthy server directly above, below, left, or right of a down server also goes down. Empty slots stop the spread.

Write minutesUntilAllDown(rack) that returns how many minutes pass until no healthy server is left, or -1 if some healthy server will never go down. If there are no healthy servers at the start, return 0.

Example 1

Input: rack = ["Xo.","oo.",".oo"]
Output: 4

Minute 1: (0, 1) and (1, 0). Minute 2: (1, 1). Minute 3: (2, 1). Minute 4: (2, 2).

Example 2

Input: rack = ["Xo.o"]
Output: -1

The empty slot keeps the last server healthy forever.

Constraints

  • 1 <= rack.length, rack[i].length <= 100
  • Each cell is "X", "o", or ".".
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).