> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/array/prefix-sum/2483.-minimum-penalty-for-a-shop.md).

# 2483. Minimum Penalty for a Shop

## Problem Statement

<br>

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 `ith` character is `'Y'`, it means that customers come at the `ith` hour
* whereas `'N'` indicates that no customers come at the `ith` 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 be closed to incur a **minimum** penalty.*

**Note** that if a shop closes at the `jth` hour, it means the shop is closed at the hour `j`.

&#x20;

**Example 1:**

<pre><code><strong>Input: customers = "YYNY"
</strong><strong>Output: 2
</strong><strong>Explanation: 
</strong>- 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.
</code></pre>

**Example 2:**

<pre><code><strong>Input: customers = "NNNNN"
</strong><strong>Output: 0
</strong><strong>Explanation: It is best to close the shop at the 0th hour as no customers arrive.
</strong></code></pre>

**Example 3:**

<pre><code><strong>Input: customers = "YYYY"
</strong><strong>Output: 4
</strong><strong>Explanation: It is best to close the shop at the 4th hour as customers arrive at each hour.
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= customers.length <= 105`
* `customers` consists only of characters `'Y'` and `'N'`.

## Intuition

```
Approach:

If we are closing a shop at certain index
We want no N's ahead of it, as the shop will remain open and no customer

And second thing,
We dont want Y's starting from that index, As the shop would close and the customers
Would come

Hence we maintain , a prefix(for N) and suffix(for Y) array
To keep track of minimum sum 

Return the min index
```

### Links

<https://leetcode.com/problems/minimum-penalty-for-a-shop/description/>

### Video Links

<https://www.youtube.com/watch?v=iB0IGr-Huu0&ab_channel=AryanMittal>

### Approach 1:

```
Prefix sum
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int bestClosingTime(string arr) {
        int n = arr.size();
        vector<int> pre(n+1,0), suf(n+1,0);

        for(int i=1; i<=n; i++){
            if(arr[i-1] == 'N')
                pre[i] = pre[i-1] + 1;
            else
                pre[i] = pre[i-1];
        }

        for(int i=n-1; i>=0; i--){
            if(arr[i] == 'Y')
                suf[i] = suf[i+1] + 1;
            else
                suf[i] = suf[i+1];
        }

        int ans=INT_MAX;
        int index;

        for(int i=0; i<=n; i++){
            if(pre[i] + suf[i] < ans){
                ans = pre[i] + suf[i];
                index = i;
            }
        }

        return index;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
