Minimum Penalty for a Shop - Complete Solution Guide
Minimum Penalty for a Shop is LeetCode problem 2483, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 i th character is 'Y' , it means that customers come at the i th hour whereas 'N' indicates that no customers come at the i th hour. If the shop closes at the j th 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 cu
Detailed Explanation
The problem asks us to find the optimal closing time for a shop to minimize the penalty based on customer visits. We're given a string 'customers' where 'Y' indicates customers visited during that hour and 'N' indicates no customers visited. The penalty is calculated as follows: for every hour the shop is open when no customers come, the penalty increases by 1, and for every hour the shop is closed when customers come, the penalty increases by 1. The goal is to return the earliest hour at which the shop should be closed to incur the minimum penalty.
Solution Approach
The provided solution uses a single pass through the 'customers' string to calculate a 'current_score'. For each 'Y', the score is incremented, and for each 'N', it is decremented. The 'max_score' keeps track of the highest score encountered so far, and 'best_hour' is updated whenever a new 'max_score' is found. Initially, the max_score and best_hour are set to 0, representing closing at the beginning. The algorithm efficiently finds the hour that maximizes the current_score, effectively minimizing the penalty.
Step-by-Step Algorithm
- Step 1: Initialize max_score, best_hour, and current_score to 0.
- Step 2: Iterate through the 'customers' string from left to right.
- Step 3: For each character, if it's 'Y', increment current_score; otherwise, decrement current_score.
- Step 4: If the current_score is greater than the max_score, update max_score to current_score and best_hour to the current hour index plus 1.
- Step 5: After iterating through the entire string, return best_hour.
Key Insights
- Insight 1: Instead of calculating the penalty for each possible closing time independently, we can use a running score (prefix sum technique) to efficiently update the penalty as we iterate through the hours.
- Insight 2: The problem is essentially finding the prefix where we maximize the difference between the number of 'Y's encountered before the prefix and the number of 'N's encountered after the prefix (or vice versa minimizing the penalty).
- Insight 3: Closing at hour 0 means the shop is immediately closed, and closing at the last hour means the shop stays open throughout the entire period.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String, Prefix Sum.
Companies
Asked at: Stripe.