Skip to main content

2483.js

/** ------------------------------------------------------------------------------
*
* 2483. Minimum Penalty for a Shop
* Topics: String, Prefix Sum
* https://leetcode.com/problems/minimum-penalty-for-a-shop/description/?envType=daily-question&envId=2025-12-26
*
------------------------------------------------------------------------------ */
/**
* @param {string} customers
* @return {number}
*/
var bestClosingTime = function (customers) {
let penalty = 965

let minIndex = 0
let minPenalty = penalty

for (let i = 0; i < customers.length; i++) {
penalty = penalty + (customers[i] === "N" ? 1 : -1)

if (penalty < minPenalty) {
minPenalty = penalty
minIndex = i + 1
}
}

return minIndex
}