Find the Best Server Shutdown Time
Problem statement
A server log is a binary string. Each character describes one time slot:
0means the server was running.1means the server was down.
You may shut down the server immediately before any slot, including before the first slot or after the last slot. If you shut it down at index t, the penalty is:
- one point for every down slot before
t; and - one point for every running slot at or after
t.
Return the shutdown index with the smallest penalty. If several indices have the same minimum penalty, return the earliest one.
What the interview report shared
The reported progression first scored a supplied shutdown time, then reused that penalty rule to choose a minimum-penalty shutdown position. Both endpoints were valid choices.
Function
bestShutdownTime(log: String) → intExamples
Example 1
log = "0011"return = 2At index 2, both running slots occur before shutdown and both down slots occur after shutdown, so the penalty is 0.
Example 2
log = "0101"return = 1Indices 1 and 3 both have penalty 1. The earlier index is returned.
Constraints
1 <= log.length <= 2 * 10^5.logcontains only0and1.- For this exercise, equal minimum penalties use the earliest shutdown index.