/
MinimumPenaltyForAShop.java
44 lines (40 loc) · 1.21 KB
/
MinimumPenaltyForAShop.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// https://leetcode.com/problems/minimum-penalty-for-a-shop
class Solution {
public int bestClosingTime(String customers) {
int n = customers.length();
int[] open = new int[n];
int[] closed = new int[n];
for (int i = 0; i < n; i++) {
char c = customers.charAt(i);
if (i == 0) {
if (c == 'Y') {
closed[i] = 1;
} else {
open[i] = 1;
}
} else {
if (c == 'Y') {
closed[i] = closed[i - 1] + 1;
open[i] = open[i - 1];
} else {
open[i] = open[i - 1] + 1;
closed[i] = closed[i - 1];
}
}
}
int minPenalty = Integer.MAX_VALUE, res = -1;
for (int i = 0; i <= n; i++) {
int penalty;
if (i == 0) {
penalty = closed[n - 1];
} else {
penalty = closed[n - 1] - closed[i - 1] + open[i - 1];
}
if (penalty < minPenalty) {
minPenalty = penalty;
res = i;
}
}
return res;
}
}