-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path2391
43 lines (39 loc) · 1.11 KB
/
2391
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
class Solution {
public:
int garbageCollection(vector<string>& garbage, vector<int>& travel) {
unordered_map<int , vector<int>>mp;
vector<int>last_index(3);
for(int i=0; i<garbage.size(); i++){
int m = 0 , p= 0, g =0;
for(auto ch :garbage[i]){
if(ch=='M')m++;
else if(ch=='P')p++;
else g++;
}
mp[i].push_back(m); // 0th index for metal
mp[i].push_back(p); // 1st index for paper
mp[i].push_back(g); // 2nd index for glass
if(m>0)last_index[0] = i;
if(p>0)last_index[1] = i;
if(g>0)last_index[2] = i;
}
// for metal
int ans = 0;
travel.insert(travel.begin(), 0);
for(int i=0; i<=last_index[0]; i++){
ans += mp[i][0];
ans += travel[i];
}
// for paper
for(int i=0; i<=last_index[1]; i++){
ans += mp[i][1];
ans += travel[i];
}
// for glass
for(int i=0; i<=last_index[2]; i++){
ans += mp[i][2];
ans += travel[i];
}
return ans;
}
};