Find the earliest closing hour that minimizes a shop's penalty

Quick Overview

Given an hourly log of whether customers showed up, choose the hour to close a shop so that empty open hours plus missed customer hours is as small as possible, and return the earliest such hour. Tests turning a brute-force scan into a one-pass incremental update and handling ties correctly.

Find the earliest closing hour that minimizes a shop's penalty

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given the customer visit log of a shop as a 0-indexed string `customers` of length `n`, made only of the characters `'N'` and `'Y'`: - If the `i`th character is `'Y'`, customers come at the `i`th hour. - If the `i`th character is `'N'`, 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 close to incur the minimum penalty. ### Function Signature ```python def best_closing_time(customers: str) -> int: ``` ### Rules - Closing at hour `j` means the shop is open during hours `0` to `j - 1` and closed during hours `j` to `n - 1`. - `j` can be any value from 0 to `n` inclusive. `j = 0` means the shop never opens, and `j = n` means it stays open for every hour in the log. - If several closing hours give the same minimum penalty, return the smallest one. ### Constraints - `1 <= len(customers) <= 10^5` - `customers` consists only of the characters `'Y'` and `'N'`. ### Examples ```text Input: customers = "YYNY" Output: 2 ``` The penalties for closing at hours 0, 1, 2, 3 and 4 are 3, 2, 1, 2 and 1. Closing at hour 2 keeps the shop open for the two `'Y'` hours (no penalty) and closed for hours 2 and 3, where only the `'Y'` at hour 3 costs 1. Hour 4 also gives 1, but 2 is earlier. ```text Input: customers = "NNNNN" Output: 0 ``` No customers ever come, so closing at hour 0 avoids every penalty. Each later closing hour adds 1 for each empty open hour. ```text Input: customers = "YYYY" Output: 4 ``` Every hour has customers, so the penalty drops from 4 at hour 0 to 0 at hour 4, which means staying open for the whole log.

Overview: Given an hourly log of whether customers showed up, choose the hour to close a shop so that empty open hours plus missed customer hours is as small as possible, and return the earliest such hour. Tests turning a brute-force scan into a one-pass incremental update and handling ties correctly.

Stripe logo
Stripe
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given the customer visit log of a shop as a 0-indexed string customers of length n, made only of the characters 'N' and 'Y':

  • If the i th character is 'Y' , customers come at the i th hour.
  • If the i th character is 'N' , no customers come at the i th hour.

If the shop closes at the jth 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 close to incur the minimum penalty.

Function Signature

def best_closing_time(customers: str) -> int:

Rules

  • Closing at hour j means the shop is open during hours 0 to j - 1 and closed during hours j to n - 1 .
  • j can be any value from 0 to n inclusive. j = 0 means the shop never opens, and j = n means it stays open for every hour in the log.
  • If several closing hours give the same minimum penalty, return the smallest one.

Constraints

  • 1 <= len(customers) <= 10^5
  • customers consists only of the characters 'Y' and 'N' .

Examples

Input: customers = "YYNY"
Output: 2

The penalties for closing at hours 0, 1, 2, 3 and 4 are 3, 2, 1, 2 and 1. Closing at hour 2 keeps the shop open for the two 'Y' hours (no penalty) and closed for hours 2 and 3, where only the 'Y' at hour 3 costs 1. Hour 4 also gives 1, but 2 is earlier.

Input: customers = "NNNNN"
Output: 0

No customers ever come, so closing at hour 0 avoids every penalty. Each later closing hour adds 1 for each empty open hour.

Input: customers = "YYYY"
Output: 4

Every hour has customers, so the penalty drops from 4 at hour 0 to 0 at hour 4, which means staying open for the whole log.