> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Penalty for a Shop Python Solution

> Tested Python solution for LeetCode 2483 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2483, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/minimum-penalty-for-a-shop/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2483   # by problem number
lcpy gen -s minimum_penalty_for_a_shop   # by problem name
```

## 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

```
Input: customers = "YYNY"
Output: 2
Explanation:
- Closing the shop at the 0th hour incurs in 1+1+0+1 = 3 penalty.
- Closing the shop at the 1st hour incurs in 0+1+0+1 = 2 penalty.
- Closing the shop at the 2nd hour incurs in 0+0+0+1 = 1 penalty.
- Closing the shop at the 3rd hour incurs in 0+0+1+1 = 2 penalty.
- Closing the shop at the 4th hour incurs in 0+0+1+0 = 1 penalty.
Closing the shop at 2nd or 4th hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2.
```

```
Input: customers = "NNNNN"
Output: 0
Explanation: It is best to close the shop at the 0th hour as no customers arrive.
```

```
Input: customers = "YYYY"
Output: 4
Explanation: It is best to close the shop at the 4th hour as customers arrive at each hour.
```

### Constraints

* `1 <= customers.length <= 10<sup>5</sup>`
* `customers` consists only of characters `'Y'` and `'N'`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_penalty_for_a_shop/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_penalty_for_a_shop/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def best_closing_time(self, customers: str) -> int:
        penalty = customers.count("Y")
        best_penalty = penalty
        best_hour = 0
        for hour, cust in enumerate(customers, start=1):
            if cust == "Y":
                penalty -= 1
            else:
                penalty += 1
            if penalty < best_penalty:
                best_penalty = penalty
                best_hour = hour
        return best_hour
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.