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.

A shop records its customer visits 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 during hour `i`. - If the `i`th character is `'N'`, no customers come during hour `i`. If the shop closes at hour `j` (`0 <= j <= n`), it is open during hours `0` to `j - 1` and closed during hours `j` to `n - 1`. So `j = 0` means the shop never opens, and `j = n` means it stays open for every hour in the log. The penalty for closing at hour `j` 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. Implement `best_closing_time(customers)`, which returns the earliest hour at which the shop must close to incur the minimum penalty. 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'`. - The answer is an integer between `0` and `n` inclusive, so it is at most `10^5` and fits in a 32-bit signed integer (`int` in Java and C++). Example 1: Input: customers = "YYNY" Output: 2 Closing at hours 0, 1, 2, 3 and 4 gives penalties 3, 2, 1, 2 and 1. Hours 2 and 4 both give the minimum penalty 1, and 2 is earlier. Example 2: Input: customers = "NNNNN" Output: 0 No customers ever come, so closing at hour 0 avoids every penalty. Each later closing hour adds 1 for every empty open hour.

Constraints

  • 1 <= len(customers) <= 10^5
  • customers consists only of the characters 'Y' and 'N'.
  • The answer is an integer j with 0 <= j <= len(customers), so it is at most 10^5 and fits in a 32-bit signed integer.

Examples

Input: ('Y',)

Expected Output: 1

Explanation: Minimum valid, one 'Y' hour: staying open for it (j = 1) costs 0, closing at hour 0 costs 1.

Input: ('N',)

Expected Output: 0

Explanation: Minimum valid, one 'N' hour: closing at hour 0 costs 0, j = 1 costs 1.

Hints

  1. Closing at hour j splits the log into an open part (hours before j) and a closed part (hour j and later). Each part is charged for a different character.
  2. Both ends count: j = 0 (never open) and j = n (open for the whole log) are valid answers.
  3. Several hours can tie for the minimum penalty. Make sure your comparison keeps the earliest one.

Earliest Closing Hour With the Minimum Shop Penalty

mediumStripeSoftware Engineer

A shop records its customer visits as a 0-indexed string customers of length n, made only of the characters 'N' and 'Y':

  • If the ith character is 'Y', customers come during hour i.
  • If the ith character is 'N', no customers come during hour i.

If the shop closes at hour j (0 <= j <= n), it is open during hours 0 to j - 1 and closed during hours j to n - 1. So j = 0 means the shop never opens, and j = n means it stays open for every hour in the log. The penalty for closing at hour j 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.

Implement best_closing_time(customers), which returns the earliest hour at which the shop must close to incur the minimum penalty. 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'.
  • The answer is an integer between 0 and n inclusive, so it is at most 10^5 and fits in a 32-bit signed integer (int in Java and C++).

Example 1:

Input: customers = "YYNY" Output: 2

Closing at hours 0, 1, 2, 3 and 4 gives penalties 3, 2, 1, 2 and 1. Hours 2 and 4 both give the minimum penalty 1, and 2 is earlier.

Example 2:

Input: customers = "NNNNN" Output: 0

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

Examples

Example 1
Input
('Y',)
Output
1
Notes
Minimum valid, one 'Y' hour: staying open for it (j = 1) costs 0, closing at hour 0 costs 1.
Example 2
Input
('N',)
Output
0
Notes
Minimum valid, one 'N' hour: closing at hour 0 costs 0, j = 1 costs 1.

Constraints

  • •1 <= len(customers) <= 10^5
  • •customers consists only of the characters 'Y' and 'N'.
  • •The answer is an integer j with 0 <= j <= len(customers), so it is at most 10^5 and fits in a 32-bit signed integer.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def best_closing_time(customers) -> int:
Sample call
best_closing_time('Y')
Language
Case 1
Input
('Y',)
Expected
1
Case 2
Input
('N',)
Expected
0

Browse More Questions

Show the approach

Approach

Let P(j) be the penalty for closing at hour j: the number of 'N' characters in customers[0:j] (open but empty) plus the number of 'Y' characters in customers[j:n] (closed while customers come). P(0) is the total number of 'Y'. Moving the closing hour from j to j + 1 moves hour j from the closed part to the open part, so P drops by 1 when customers[j] is 'Y' and rises by 1 when it is 'N'. Therefore P(j) = P(0) - score(j), where score(j) is the count of 'Y' minus the count of 'N' among the first j characters, and minimizing P is the same as maximizing score.

The solution scans the string once with a running score that starts at 0 for j = 0. After reading character i it holds score(i + 1), and it replaces the best hour with i + 1 only when that score is strictly greater than the best seen so far. Invariant: after reading i characters, answer is the smallest j in [0, i] whose score is the maximum over that range. The strict comparison keeps the earliest hour on ties, which is the tie-break the problem fixes; a >= comparison would return the latest tied hour instead.

Edge cases: an all-'N' log never lifts the score above 0, so the answer is 0; an all-'Y' log raises it at every step, so the answer is n. The score stays between -n and n, at most 10^5 in absolute value, so 32-bit integers are enough in every language.

Time complexity:
O(n)
Space complexity:
O(1)