Skip to content

Commit 3a02fe1

Browse files
Create 2483-minimum-penalty-for-a-shop.java
1 parent ee2d53a commit 3a02fe1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int bestClosingTime(String cust) {
3+
int n = cust.length();
4+
int[] pre_n = new int[n+1];
5+
int[] post_y = new int[n+1];
6+
7+
for(int i = 1; i <= n; i++){
8+
pre_n[i] = pre_n[i-1];
9+
if(cust.charAt(i-1) == 'N')
10+
pre_n[i]++;
11+
}
12+
for(int i = n-1; i >= 0; i--){
13+
post_y[i] = post_y[i+1];
14+
if(cust.charAt(i) == 'Y')
15+
post_y[i]++;
16+
}
17+
18+
int min_penalty = Integer.MAX_VALUE, idx = 0;
19+
20+
for(int i = 0; i <= n; i++){
21+
int penalty = pre_n[i] + post_y[i];
22+
if(penalty < min_penalty){
23+
min_penalty = penalty;
24+
idx = i;
25+
}
26+
}
27+
return idx;
28+
}
29+
}

0 commit comments

Comments
 (0)