java.lang.Object
g2401_2500.s2483_minimum_penalty_for_a_shop.Solution

public class Solution extends Object
2483 - Minimum Penalty for a Shop.<p>Medium</p> <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>'N'</code> and <code>'Y'</code>:</p> <ul> <li>if the <code>i<sup>th</sup></code> character is <code>'Y'</code>, it means that customers come at the <code>i<sup>th</sup></code> hour</li> <li>whereas <code>'N'</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 <= j <= 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><strong>Example 1:</strong></p> <p><strong>Input:</strong> customers = &ldquo;YYNY&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <ul> <li>Closing the shop at the 0<sup>th</sup> hour incurs in 1+1+0+1 = 3 penalty.</li> <li>Closing the shop at the 1<sup>st</sup> hour incurs in 0+1+0+1 = 2 penalty.</li> <li>Closing the shop at the 2<sup>nd</sup> hour incurs in 0+0+0+1 = 1 penalty.</li> <li>Closing the shop at the 3<sup>rd</sup> hour incurs in 0+0+1+1 = 2 penalty.</li> <li>Closing the shop at the 4<sup>th</sup> hour incurs in 0+0+1+0 = 1 penalty.</li> </ul> <p>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.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> customers = &ldquo;NNNNN&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> It is best to close the shop at the 0<sup>th</sup> hour as no customers arrive.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> customers = &ldquo;YYYY&rdquo;</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> It is best to close the shop at the 4<sup>th</sup> hour as customers arrive at each hour.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= customers.length <= 10<sup>5</sup></code></li> <li><code>customers</code> consists only of characters <code>'Y'</code> and <code>'N'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • bestClosingTime

      public int bestClosingTime(String customers)