2483. Minimum Penalty for a Shop LeetCode Solution

In this guide, you will get 2483. Minimum Penalty for a Shop LeetCode Solution with the best time and space complexity. The solution to Minimum Penalty for a Shop problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Penalty for a Shop solution in C++
  4. Minimum Penalty for a Shop solution in Java
  5. Minimum Penalty for a Shop solution in Python
  6. Additional Resources
2483. Minimum Penalty for a Shop LeetCode Solution image

Problem Statement of Minimum Penalty for a Shop

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.

Example 1:

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.

Example 2:

Input: customers = “NNNNN”
Output: 0
Explanation: It is best to close the shop at the 0th hour as no customers arrive.
Example 3:

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 <= 105
customers consists only of characters 'Y' and 'N'.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2483. Minimum Penalty for a Shop LeetCode Solution in C++

class Solution {
 public:
  int bestClosingTime(string customers) {
    // Instead of computing the minimum penalty, we can compute the maximum
    // profit.
    int ans = 0;
    int profit = 0;
    int maxProfit = 0;

    for (int i = 0; i < customers.length(); ++i) {
      profit += customers[i] == 'Y' ? 1 : -1;
      if (profit > maxProfit) {
        maxProfit = profit;
        ans = i + 1;
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2483. Minimum Penalty for a Shop LeetCode Solution in Java

class Solution {
  public int bestClosingTime(String customers) {
    // Instead of computing the minimum penalty, we can compute the maximum profit.
    int ans = 0;
    int profit = 0;
    int maxProfit = 0;

    for (int i = 0; i < customers.length(); ++i) {
      profit += customers.charAt(i) == 'Y' ? 1 : -1;
      if (profit > maxProfit) {
        maxProfit = profit;
        ans = i + 1;
      }
    }

    return ans;
  }
}
// code provided by PROGIEZ

2483. Minimum Penalty for a Shop LeetCode Solution in Python

class Solution:
  def bestClosingTime(self, customers: str) -> int:
    # Instead of computing the minimum penalty, we can compute the maximum profit.
    ans = 0
    profit = 0
    maxProfit = 0

    for i, customer in enumerate(customers):
      profit += 1 if customer == 'Y' else -1
      if profit > maxProfit:
        maxProfit = profit
        ans = i + 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.