-
Notifications
You must be signed in to change notification settings - Fork 3
/
2391.cpp
32 lines (32 loc) · 909 Bytes
/
2391.cpp
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
class Solution {
public:
int garbageCollection(vector<string>& garbage, vector<int>& travel) {
int n = garbage.size();
int metalLast = 0;
int paperLast = 0;
int glassLast = 0;
vector<int> prefixSum(n, 0);
for (int i = 1; i < n; ++i) {
prefixSum[i] = prefixSum[i - 1] + travel[i - 1];
}
int res = 0;
for (int i = 0; i < n; ++i) {
res += garbage[i].size();
for (auto c : garbage[i]) {
if (c == 'M') {
metalLast = i;
}
else if (c == 'P') {
paperLast = i;
}
else {
glassLast = i;
}
}
}
res += prefixSum[metalLast];
res += prefixSum[paperLast];
res += prefixSum[glassLast];
return res;
}
};