Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
You are given the customer visit log of a shop represented by a 0-indexed string customers consisting only of characters 'N' and 'Y':
ith character is 'Y', it means that customers come at the ith hour'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:
1.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 <= 105customers consists only of characters 'Y' and 'N'.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"YYNY"
"NNNNN"
"YYYY"
grid-game)minimum-amount-of-damage-dealt-to-bob)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2483: Minimum Penalty for a Shop
class Solution {
public int bestClosingTime(String customers) {
int n = customers.length();
int ans = 0, cost = 0;
for (int i = 0; i < n; i++) {
if (customers.charAt(i) == 'Y') {
cost++;
}
}
int mn = cost;
for (int j = 1; j <= n; j++) {
cost += customers.charAt(j - 1) == 'N' ? 1 : -1;
if (cost < mn) {
ans = j;
mn = cost;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2483: Minimum Penalty for a Shop
func bestClosingTime(customers string) int {
ans := 0
cost := strings.Count(customers, "Y")
mn := cost
for j := 1; j <= len(customers); j++ {
c := customers[j-1]
if c == 'N' {
cost++
} else {
cost--
}
if cost < mn {
ans = j
mn = cost
}
}
return ans
}
# Accepted solution for LeetCode #2483: Minimum Penalty for a Shop
class Solution:
def bestClosingTime(self, customers: str) -> int:
ans = 0
mn = cost = customers.count("Y")
for j, c in enumerate(customers, 1):
cost += 1 if c == "N" else -1
if cost < mn:
ans, mn = j, cost
return ans
// Accepted solution for LeetCode #2483: Minimum Penalty for a Shop
impl Solution {
pub fn best_closing_time(customers: String) -> i32 {
let bytes = customers.as_bytes();
let mut cost: i32 = bytes.iter().filter(|&&c| c == b'Y').count() as i32;
let mut mn = cost;
let mut ans: i32 = 0;
for j in 1..=bytes.len() {
let c = bytes[j - 1];
if c == b'N' {
cost += 1;
} else {
cost -= 1;
}
if cost < mn {
mn = cost;
ans = j as i32;
}
}
ans
}
}
// Accepted solution for LeetCode #2483: Minimum Penalty for a Shop
function bestClosingTime(customers: string): number {
let ans = 0;
let cost = 0;
for (const ch of customers) {
if (ch === 'Y') {
cost++;
}
}
let mn = cost;
for (let j = 1; j <= customers.length; j++) {
const c = customers[j - 1];
cost += c === 'N' ? 1 : -1;
if (cost < mn) {
mn = cost;
ans = j;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.