LeetCode 2483, Medium. Topics: String, Prefix Sum. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:
Problem
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<sup>th</sup> character is 'Y', it means that customers come at the i<sup>th</sup> hour
- whereas
'N' indicates that no customers come at the i<sup>th</sup> hour.
If the shop closes at the j<sup>th</sup> 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<sup>th</sup> hour, it means the shop is closed at the hour j.
Examples
Constraints
1 <= customers.length <= 10<sup>5</sup>
customers consists only of characters 'Y' and 'N'.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026