<p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p>

<ul>
	<li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customers come at the <code>i<sup>th</sup></code> hour</li>
	<li>whereas <code>&#39;N&#39;</code> indicates that no customers come at the <code>i<sup>th</sup></code> hour.</li>
</ul>

<p>If the shop closes at the <code>j<sup>th</sup></code> hour (<code>0 &lt;= j &lt;= n</code>), the <strong>penalty</strong> is calculated as follows:</p>

<ul>
	<li>For every hour when the shop is open and no customers come, the penalty increases by <code>1</code>.</li>
	<li>For every hour when the shop is closed and customers come, the penalty increases by <code>1</code>.</li>
</ul>

<p>Return<em> the <strong>earliest</strong> hour at which the shop must be closed to incur a <strong>minimum</strong> penalty.</em></p>

<p><strong>Note</strong> that if a shop closes at the <code>j<sup>th</sup></code> hour, it means the shop is closed at the hour <code>j</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> customers = &quot;YYNY&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
- Closing the shop at the 0<sup>th</sup> hour incurs in 1+1+0+1 = 3 penalty.
- Closing the shop at the 1<sup>st</sup> hour incurs in 0+1+0+1 = 2 penalty.
- Closing the shop at the 2<sup>nd</sup> hour incurs in 0+0+0+1 = 1 penalty.
- Closing the shop at the 3<sup>rd</sup> hour incurs in 0+0+1+1 = 2 penalty.
- Closing the shop at the 4<sup>th</sup> hour incurs in 0+0+1+0 = 1 penalty.
Closing the shop at 2<sup>nd</sup> or 4<sup>th</sup> hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2.
</pre>

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= customers.length &lt;= 10<sup>5</sup></code></li>
	<li><code>customers</code> consists only of characters <code>&#39;Y&#39;</code> and <code>&#39;N&#39;</code>.</li>
</ul>

class Solution {
public:
vector <int> close_shop;
vector <int> open_shop;



// int open_shop(string customers , int i)
// {
// if(i<0) return 0;
// int count =0;
// while(i>=0)
// {
//     if( customers[i] == 'N')
//     count +=1;

//     i-=1;
// }

// return count;


// }

//    int close_shop(string customers , int i)
//    {
//    int n = customers.size();
//    int count =0;
//    if(i ==n) return 0;
//    while(i<n)
//    {
//     if( customers[i] == 'Y')
//     count +=1;

//     i+=1;
//    }
// return count;
//    }
   
   
    int bestClosingTime(string customers) {
    
    
    //  memory limit exceeded
    
   int n = customers.size();
     int check = INT_MAX , indx = -1;
//     for(int i=0; i<=n; i++)
//     {
//     int close = close_shop(customers , i);
//     int open = open_shop(customers , i-1);

//     if( (close + open) < check)
//     {
//         check = close+open;
//         indx = i;
//     }

//     }

// return indx;


for(int i=0; i<n;  i++)
{
    if(customers[i] == 'Y')
    close_shop.push_back(i);

    else
    open_shop.push_back(i);   // not come
}


if(close_shop.size()==0) return 0;  // NNNN

if(close_shop.size() ==n) return n; // yyyy

int not_come = open_shop.size();

 int open_count = 0;

for(int i =0; i< n; i++)
{ 

int close_count = 0;  
if(  close_shop.size() > 0 )
{
if(i > close_shop[0] )
close_shop.erase(close_shop.begin());

close_count = close_shop.size();

}

 if(i >0  and open_shop.size() >0)
{

if(i-1 == open_shop[0]) {
open_count +=1;

open_shop.erase(open_shop.begin());

}
}

 if((open_count+close_count )  < check)
 {
    check = open_count+close_count;
    indx = i;
 }


cout<<check<<"   ";
}


// last when shop close at i ==n

// no of customer not come , when shop open

//cout<<check<<"   "<<not_come;

if(not_come <check )
{
    return n;
}

 return indx;       

}
};
22400