Skip to content

Commit 071d6c4

Browse files
committed
feat: solve No.2391
1 parent 6ec784c commit 071d6c4

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# 2391. Minimum Amount of Time to Collect Garbage
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, String, Prefix Sum.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given a **0-indexed** array of strings `garbage` where `garbage[i]` represents the assortment of garbage at the `ith` house. `garbage[i]` consists only of the characters `'M'`, `'P'` and `'G'` representing one unit of metal, paper and glass garbage respectively. Picking up **one** unit of any type of garbage takes `1` minute.
10+
11+
You are also given a **0-indexed** integer array `travel` where `travel[i]` is the number of minutes needed to go from house `i` to house `i + 1`.
12+
13+
There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house `0` and must visit each house **in order**; however, they do **not** need to visit every house.
14+
15+
Only **one** garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks **cannot** do anything.
16+
17+
Return** the **minimum** number of minutes needed to pick up all the garbage.**
18+
19+
 
20+
Example 1:
21+
22+
```
23+
Input: garbage = ["G","P","GP","GG"], travel = [2,4,3]
24+
Output: 21
25+
Explanation:
26+
The paper garbage truck:
27+
1. Travels from house 0 to house 1
28+
2. Collects the paper garbage at house 1
29+
3. Travels from house 1 to house 2
30+
4. Collects the paper garbage at house 2
31+
Altogether, it takes 8 minutes to pick up all the paper garbage.
32+
The glass garbage truck:
33+
1. Collects the glass garbage at house 0
34+
2. Travels from house 0 to house 1
35+
3. Travels from house 1 to house 2
36+
4. Collects the glass garbage at house 2
37+
5. Travels from house 2 to house 3
38+
6. Collects the glass garbage at house 3
39+
Altogether, it takes 13 minutes to pick up all the glass garbage.
40+
Since there is no metal garbage, we do not need to consider the metal garbage truck.
41+
Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
42+
```
43+
44+
Example 2:
45+
46+
```
47+
Input: garbage = ["MMM","PGM","GP"], travel = [3,10]
48+
Output: 37
49+
Explanation:
50+
The metal garbage truck takes 7 minutes to pick up all the metal garbage.
51+
The paper garbage truck takes 15 minutes to pick up all the paper garbage.
52+
The glass garbage truck takes 15 minutes to pick up all the glass garbage.
53+
It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
54+
```
55+
56+
 
57+
**Constraints:**
58+
59+
60+
61+
- `2 <= garbage.length <= 105`
62+
63+
- `garbage[i]` consists of only the letters `'M'`, `'P'`, and `'G'`.
64+
65+
- `1 <= garbage[i].length <= 10`
66+
67+
- `travel.length == garbage.length - 1`
68+
69+
- `1 <= travel[i] <= 100`
70+
71+
72+
73+
## Solution
74+
75+
```javascript
76+
/**
77+
* @param {string[]} garbage
78+
* @param {number[]} travel
79+
* @return {number}
80+
*/
81+
var garbageCollection = function(garbage, travel) {
82+
var res = 0;
83+
var m = 0;
84+
var p = 0;
85+
var g = 0;
86+
for (var i = 0; i < garbage.length; i++) {
87+
const num = count(garbage[i]);
88+
m += (travel[i - 1] || 0);
89+
p += (travel[i - 1] || 0);
90+
g += (travel[i - 1] || 0);
91+
if (num.m) {
92+
res += m + num.m;
93+
m = 0;
94+
}
95+
if (num.p) {
96+
res += p + num.p;
97+
p = 0;
98+
}
99+
if (num.g) {
100+
res += g + num.g;
101+
g = 0;
102+
}
103+
}
104+
return res;
105+
};
106+
107+
var count = function(str) {
108+
var res = { m: 0, p: 0, g: 0 };
109+
for (var i = 0; i < str.length; i++) {
110+
if (str[i] === 'M') {
111+
res.m++;
112+
} else if (str[i] === 'P') {
113+
res.p++;
114+
} else {
115+
res.g++;
116+
}
117+
}
118+
return res;
119+
};
120+
```
121+
122+
**Explain:**
123+
124+
nope.
125+
126+
**Complexity:**
127+
128+
* Time complexity : O(n * m).
129+
* Space complexity : O(1).

0 commit comments

Comments
 (0)