Tap here to switch tabs
Problem
Submissions

Problem: Minimum Penalty for a Shop

med
30 min
Try to solve the Minimum Penalty for a Shop problem.

Statement

You are given a customer visit log represented by a string customers of length nn. For each hour ii (0≤i<n0 \leq i < n), the character customers[i] indicates whether a customer arrived:

  • 'Y' indicates that a customer arrived at the iextthi^{ ext{th}} hour.

  • 'N' indicates that no customer arrived at the iextthi^{ ext{th}} hour.

The shop can be closed at any hour jj between 00 and nn, inclusive. Closing the shop incurs a penalty calculated as follows:

  • For each hour ii where 0≤i<j0 \leq i < j, if the shop is open and no customer arrives (customers[i] == 'N'), the penalty increases by 11.

  • For each hour ii where j≤i<nj \leq i < n, if the shop is closed and a customer arrives (customers[i] == 'Y'), the penalty increases by 11.

Return the earliest closing hour jj that achieves the minimum possible penalty.

Constraints:

  • 1≤1 \leq customers.length ≤105\leq 10^5

  • customers consists only of characters 'Y' and 'N'.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Penalty for a Shop

med
30 min
Try to solve the Minimum Penalty for a Shop problem.

Statement

You are given a customer visit log represented by a string customers of length nn. For each hour ii (0≤i<n0 \leq i < n), the character customers[i] indicates whether a customer arrived:

  • 'Y' indicates that a customer arrived at the iextthi^{ ext{th}} hour.

  • 'N' indicates that no customer arrived at the iextthi^{ ext{th}} hour.

The shop can be closed at any hour jj between 00 and nn, inclusive. Closing the shop incurs a penalty calculated as follows:

  • For each hour ii where 0≤i<j0 \leq i < j, if the shop is open and no customer arrives (customers[i] == 'N'), the penalty increases by 11.

  • For each hour ii where j≤i<nj \leq i < n, if the shop is closed and a customer arrives (customers[i] == 'Y'), the penalty increases by 11.

Return the earliest closing hour jj that achieves the minimum possible penalty.

Constraints:

  • 1≤1 \leq customers.length ≤105\leq 10^5

  • customers consists only of characters 'Y' and 'N'.