Minimum Penalty for a Shop (LeetCode 2483): Solution and Explanation
Given a log of hours with ('Y') or without ('N') customers, choose a closing hour j that minimizes idle open hours plus missed customer hours, and return the earliest such j.
Intuition
For a closing hour j, the penalty is the number of 'N' before j plus the number of 'Y' from j on. Computing that from scratch for all n + 1 hours is quadratic.
Compare two neighbouring closing hours, j and j + 1. The only difference is hour j: it is closed in the first plan and open in the second. If it has customers, opening it removes one missed customer. If it does not, opening it adds one idle hour.
So the penalty moves by exactly -1 or +1 per step. Start at 0 for hour 0, apply those steps while sweeping right, and remember the first hour where the running value is strictly the smallest. The absolute penalty is that value plus the number of 'Y', but the hour is all we need.
Approach: Sweep the closing hour with a relative penalty
- Set
penalty = 0(relative to closing at hour 0),best = 0andbestHour = 0. - For each hour
j, add -1 ifcustomers[j]is'Y', otherwise +1. The value now describes closing at hourj + 1. - If
penalty < best, setbest = penaltyandbestHour = j + 1. The strict comparison keeps the earliest hour on ties. - Return
bestHour.
Step-by-step example
customers = "YYNY"
- Closing at hour 0 misses all 3 'Y' hours: relative penalty 0, absolute 3.
- Hour 0 is 'Y': penalty becomes -1 (absolute 2), better than 0, so bestHour = 1.
- Hour 1 is 'Y': penalty becomes -2 (absolute 1), better, so bestHour = 2.
- Hour 2 is 'N': penalty becomes -1 (absolute 2), not better.
- Hour 3 is 'Y': penalty becomes -2 (absolute 1), equal to the best but not strictly lower, so bestHour stays 2. The answer is 2.
Code: Python, Java, C++
1class Solution:
2 def bestClosingTime(self, customers: str) -> int:
3 penalty = 0
4 best, best_hour = 0, 0
5 for j, c in enumerate(customers):
6 penalty += -1 if c == 'Y' else 1
7 if penalty < best:
8 best, best_hour = penalty, j + 1
9 return best_hour1class Solution {
2 public int bestClosingTime(String customers) {
3 int penalty = 0, best = 0, bestHour = 0;
4 for (int j = 0; j < customers.length(); j++) {
5 penalty += customers.charAt(j) == 'Y' ? -1 : 1;
6 if (penalty < best) {
7 best = penalty;
8 bestHour = j + 1;
9 }
10 }
11 return bestHour;
12 }
13}1class Solution {
2public:
3 int bestClosingTime(string customers) {
4 int penalty = 0, best = 0, bestHour = 0;
5 for (int j = 0; j < (int)customers.size(); j++) {
6 penalty += customers[j] == 'Y' ? -1 : 1;
7 if (penalty < best) {
8 best = penalty;
9 bestHour = j + 1;
10 }
11 }
12 return bestHour;
13 }
14};Time and space complexity
O(n)
One pass over the string with constant work per hour.
O(1)
Only three integers are kept.
Other approaches
Prefix and suffix counts (time O(n), space O(n))
Build prefixN[j] (number of 'N' before j) and suffixY[j] (number of 'Y' from j on), then take the hour minimizing their sum. Same time, O(n) extra space.
Two passes with an absolute penalty (time O(n), space O(1))
Count the 'Y' first to get the penalty at hour 0, then sweep and update it by +1 or -1. Equivalent to the relative version, with one extra pass.
Recompute per hour (time O(n^2), space O(1))
For each of the n + 1 closing hours, count the penalty directly. Quadratic and too slow for n = 10^5.
Common mistakes
- Forgetting hour n. Staying open all day is allowed, so there are n + 1 candidate closing hours.
- Using <= in the comparison. That returns the latest hour with the minimum penalty instead of the earliest.
- Assigning bestHour = j instead of j + 1. After processing hour j, the value describes closing after it, at hour j + 1.
- Mixing up the two costs: an open hour costs 1 when it is 'N', and a closed hour costs 1 when it is 'Y'.
Frequently asked questions
Why can I ignore the absolute penalty?
Every closing hour's penalty equals the number of 'Y' plus the relative value. Adding the same constant to every candidate does not change which one is smallest.
Is this the same as maximum subarray?
It is close. Scoring 'Y' as +1 and 'N' as -1, you want the prefix with the largest score, which is a best-prefix problem rather than a best-subarray one, so no reset is needed.
What does closing at hour 0 mean?
The shop never opens. Every hour with customers is then missed, so the penalty is the count of 'Y'.