2576. Minimum Penalty for a Shop
My accepted Python solution to LeetCode problem 2576, Minimum Penalty for a Shop, running in 44ms.
- Difficulty: Medium
- Python
- Runtime 44ms
- Memory 17.8MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 44ms, memory 17.8MB, accepted 2025-12-30.
class Solution:
def bestClosingTime(self, customers: str) -> int:
# Count total Y's - this is penalty if we close at hour 0
y_count = customers.count('Y')
min_penalty = y_count
best_hour = 0
current_penalty = y_count
for i, c in enumerate(customers):
if c == 'Y':
current_penalty -= 1 # One less Y after closing
else: # c == 'N'
current_penalty += 1 # One more N before closing
if current_penalty < min_penalty:
min_penalty = current_penalty
best_hour = i + 1
return best_hour