-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2483.cpp
More file actions
26 lines (26 loc) · 776 Bytes
/
2483.cpp
File metadata and controls
26 lines (26 loc) · 776 Bytes
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
class Solution {
public:
int bestClosingTime(string customers) {
int n = customers.size();
vector<int> rightY(n + 1, 0);
vector<int> leftN(n + 1, 0);
for (int i = n - 1; i >= 0; --i) {
rightY[i] = rightY[i + 1];
if (customers[i] == 'Y') rightY[i]++;
}
for (int i = 1; i <= n; ++i) {
leftN[i] = leftN[i - 1];
if (customers[i - 1] == 'N') leftN[i]++;
}
int close = -1;
int penalty = INT_MAX;
for (int i = 0; i <= n; ++i) {
int currentPenalty = rightY[i] + leftN[i];
if (currentPenalty < penalty) {
penalty = currentPenalty;
close = i;
}
}
return close;
}
};