Trains the technique from
LeetCode 2483Minimum Penalty for a ShopThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A stall keeps a log log of its hours, one character per hour: Y when a customer came in that hour and N when nobody did.
The stall may shut at the start of any hour from 0 up to the number of logged hours. Shutting at hour j means the stall was open for hours 0 through j - 1 and closed for the rest.
The cost of shutting at hour j is the number of open hours with no customer plus the number of closed hours with a customer.
Return the earliest hour whose cost is the smallest.
Example 1
Shutting at hour 2 leaves the two customer hours open and the two empty hours closed, so nothing is charged at all.
Example 2
Never opening costs two, one for each later customer hour, and nothing beats that, so the earliest such hour is reported.
Example 3
Staying open for the single customer hour costs nothing.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def best_closing_time(log: str) -> int:public int bestClosingTime(String log)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.