# 2483. Minimum Penalty for a Shop

• Time: $O(n)$
• Space: $O(1)$
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public: int bestClosingTime(string customers) { // Instead of computing the minimum penalty, we can compute the max 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; } }; 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution { public int bestClosingTime(String customers) { // Instead of computing the minimum penalty, we can compute the max 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; } } 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution: def bestClosingTime(self, customers: str) -> int: # Instead of computing the minimum penalty, we can compute the max 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