2483. Minimum Penalty for a Shop Medium

@problem@discussion
#String#Prefix Sum



1/**
2 * [2483] Minimum Penalty for a Shop
3 *
4 * You are given the customer visit log of a shop represented by a 0-indexed string customers consisting only of characters 'N' and 'Y':
5 * 
6 * 	if the i^th character is 'Y', it means that customers come at the i^th hour
7 * 	whereas 'N' indicates that no customers come at the i^th hour.
8 * 
9 * If the shop closes at the j^th hour (0 <= j <= n), the penalty is calculated as follows:
10 * 
11 * 	For every hour when the shop is open and no customers come, the penalty increases by 1.
12 * 	For every hour when the shop is closed and customers come, the penalty increases by 1.
13 * 
14 * Return the earliest hour at which the shop must be closed to incur a minimum penalty.
15 * Note that if a shop closes at the j^th hour, it means the shop is closed at the hour j.
16 *  
17 * <strong class="example">Example 1:
18 * 
19 * Input: customers = "YYNY"
20 * Output: 2
21 * Explanation: 
22 * - Closing the shop at the 0^th hour incurs in 1+1+0+1 = 3 penalty.
23 * - Closing the shop at the 1^st hour incurs in 0+1+0+1 = 2 penalty.
24 * - Closing the shop at the 2^nd hour incurs in 0+0+0+1 = 1 penalty.
25 * - Closing the shop at the 3^rd hour incurs in 0+0+1+1 = 2 penalty.
26 * - Closing the shop at the 4^th hour incurs in 0+0+1+0 = 1 penalty.
27 * Closing the shop at 2^nd or 4^th hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2.
28 * 
29 * <strong class="example">Example 2:
30 * 
31 * Input: customers = "NNNNN"
32 * Output: 0
33 * Explanation: It is best to close the shop at the 0^th hour as no customers arrive.
34 * <strong class="example">Example 3:
35 * 
36 * Input: customers = "YYYY"
37 * Output: 4
38 * Explanation: It is best to close the shop at the 4^th hour as customers arrive at each hour.
39 * 
40 *  
41 * Constraints:
42 * 
43 * 	1 <= customers.length <= 10^5
44 * 	customers consists only of characters 'Y' and 'N'.
45 * 
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/minimum-penalty-for-a-shop/
50// discuss: https://leetcode.com/problems/minimum-penalty-for-a-shop/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54impl Solution {
55    pub fn best_closing_time(customers: String) -> i32 {
56        0
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_2483() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.