lc-go/solutions/24/q2483/solution.go

30 lines
576 B
Go

// Package q2483 implements a solution for https://leetcode.com/problems/minimum-penalty-for-a-shop/
package q2483
func bestClosingTime(customers string) int {
numY := 0
for i := range len(customers) {
if customers[i] == 'Y' {
numY++
}
}
minCloseHour := 0
minPenalty := numY
penalty := numY
for i := range len(customers) {
// close at (i + 1)th hour
switch customers[i] {
case 'N':
penalty++
case 'Y':
penalty--
}
if penalty < minPenalty {
minPenalty = penalty
minCloseHour = i + 1
}
}
return minCloseHour
}
var _ = bestClosingTime