Back to DSA sheet

Minimum Penalty for a Shop

MediumArrays

A string of 'Y'/'N' tells whether a customer came each hour. If the shop closes at hour j, the penalty is the number of 'N' hours while open (indices < j) plus the number of 'Y' hours after closing (indices >= j). Closing can happen at hour 0..n. Return the earliest hour giving the minimum penalty.

Input format (stdin): a single line of 'Y'/'N'. Output the best closing hour.

Examples
Input: YYNY
Output: 2
Closing at hour 2 minimises the penalty.
Input: NNNNN
Output: 0
Best to never open.
Input: YYYY
Output: 4
Stay open the whole time.
Constraints
  • 1 <= length <= 10^5
  • characters are 'Y' or 'N'
Sheets
NeetCode 250
minimum-penalty-for-a-shop.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.