Minimum Penalty for a Shop

Medium

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:

Close at hour 2.

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

Edge case returning zero.

Input:customers = "NNYNY"
Output:4
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!