Minimum Penalty for a Shop

You are given the customer visit log of a shop represented by a 0-indexed string customers consisting only of characters 'N' and 'Y':

  • If the i^th character is 'Y', it means that customers come at the i^th hour.
  • If the i^th character is 'N', it indicates that no customers come at the i^th hour.

If the shop closes at the j^th hour (0 <= j <= n), the penalty is calculated as follows:

  • For every hour when the shop is open and no customers come, the penalty increases by 1.
  • For every hour when the shop is closed and customers come, the penalty increases by 1.

Return the earliest hour at which the shop must be closed to incur a minimum penalty.

Note that if a shop closes at the j^th hour, it means the shop is closed at the hour j.

Example 1
Inputcustomers = "YYNY"
Output2
Closing the shop at the 2^nd or 4^th hour gives a minimum penalty of 1, and 2 is earlier.
Example 2
Inputcustomers = "NNNNN"
Output0
It is best to close the shop at the 0^th hour as no customers arrive.

Constraints

  • 1 <= customers.length <= 10^5
  • customers consists only of characters 'Y' and 'N'.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate