Minimum Penalty for a Shop

MediumString

Description

Given a string customers representing customer visits (Y) or no visits (N), find the earliest hour to close that minimizes penalty.

Examples

Input:customers = "YYNY"
Output:2
Explanation:

Penalty at hour 2: 0 for hours 0-1 (open), 1 for missing Y at hour 3. Total penalty 1 is minimum.

Input:customers = "NNNNN"
Output:0
Explanation:

No customers visit at any hour. Closing at hour 0 incurs 0 penalty since there are no Ys to miss.

Input:customers = "NNYNY"
Output:0
Explanation:

Close at hour 4. If close at hour 0: penalty = 3 (miss Y at hours 2, 3, 4). If close at hour 1: penalty = 3 (1 N + miss 2 Y). If close at hour 2: penalty = 2 (2 N + miss 1 Y). If close at hour 3: penalty = 3 (2 N + miss 1 Y). If close at hour 4: penalty = 2 (2 N + 0 missed Y). If close at hour 5: penalty = 3 (3 N). The minimum penalty is 2, achieved at both hours 2 and 4, so returning the earliest: hour 4.

Constraints

  • 1 ≤ customers.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!