We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ee2d53a commit 3a02fe1Copy full SHA for 3a02fe1
java/2483-minimum-penalty-for-a-shop.java
@@ -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