##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2391.html

# 2391. Minimum Amount of Time to Collect Garbage

• Difficulty: Medium.
• Related Topics: Array, String, Prefix Sum.
• Similar Questions: .

## Problem

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.

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.

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.

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.

Return** the minimum number of minutes needed to pick up all the garbage.**

Example 1:

Input: garbage = ["G","P","GP","GG"], travel = [2,4,3]
Output: 21
Explanation:
The paper garbage truck:
1. Travels from house 0 to house 1
2. Collects the paper garbage at house 1
3. Travels from house 1 to house 2
4. Collects the paper garbage at house 2
Altogether, it takes 8 minutes to pick up all the paper garbage.
The glass garbage truck:
1. Collects the glass garbage at house 0
2. Travels from house 0 to house 1
3. Travels from house 1 to house 2
4. Collects the glass garbage at house 2
5. Travels from house 2 to house 3
6. Collects the glass garbage at house 3
Altogether, it takes 13 minutes to pick up all the glass garbage.
Since there is no metal garbage, we do not need to consider the metal garbage truck.
Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.


Example 2:

Input: garbage = ["MMM","PGM","GP"], travel = [3,10]
Output: 37
Explanation:
The metal garbage truck takes 7 minutes to pick up all the metal garbage.
The paper garbage truck takes 15 minutes to pick up all the paper garbage.
The glass garbage truck takes 15 minutes to pick up all the glass garbage.
It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.


Constraints:

• 2 <= garbage.length <= 105

• garbage[i] consists of only the letters 'M', 'P', and 'G'.

• 1 <= garbage[i].length <= 10

• travel.length == garbage.length - 1

• 1 <= travel[i] <= 100

## Solution (Java, C++, Python)

• class Solution {
public int garbageCollection(String[] garbage, int[] travel) {
int cTime = 0;
for (String str : garbage) {
cTime += str.length();
}
int n = travel.length;
for (int i = 1; i < n; i++) {
travel[i] += travel[i - 1];
}
int mT = getMostTra(garbage, 'M');
int pT = getMostTra(garbage, 'P');
int gT = getMostTra(garbage, 'G');
int m = mT <= 0 ? 0 : travel[mT - 1];
int p = pT <= 0 ? 0 : travel[pT - 1];
int g = gT <= 0 ? 0 : travel[gT - 1];
int tTime = m + p + g;
return cTime + tTime;
}

private int getMostTra(String[] garbage, char c) {
int n = garbage.length;
for (int i = n - 1; i >= 0; i--) {
if (garbage[i].indexOf(c) != -1) {
return i;
}
}
return -1;
}
}

############

class Solution {
public int garbageCollection(String[] garbage, int[] travel) {
int[] last = new int[26];
int n = garbage.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
int k = garbage[i].length();
ans += k;
for (int j = 0; j < k; ++j) {
last[garbage[i].charAt(j) - 'A'] = i;
}
}
int m = travel.length;
int[] s = new int[m + 1];
for (int i = 0; i < m; ++i) {
s[i + 1] = s[i] + travel[i];
}
for (int i : last) {
ans += s[i];
}
return ans;
}
}

• class Solution:
def garbageCollection(self, garbage: List[str], travel: List[int]) -> int:
ans = 0
pos = {}
for i, v in enumerate(garbage):
ans += len(v)
for c in v:
pos[c] = i
s = list(accumulate(travel, initial=0))
ans += sum(s[i] for i in pos.values())
return ans

############

# 2391. Minimum Amount of Time to Collect Garbage
# https://leetcode.com/problems/minimum-amount-of-time-to-collect-garbage/

class Solution:
def garbageCollection(self, garbage: List[str], travel: List[int]) -> int:
n = len(garbage)
lastM = lastP = lastG = -1
M, P, G = [0] * n, [0] * n, [0] * n

for index, gar in enumerate(garbage):
c = Counter(gar)
M[index] += c["M"]
P[index] += c["P"]
G[index] += c["G"]

if c["M"] > 0:
lastM = index

if c["P"] > 0:
lastP = index

if c["G"] > 0:
lastG = index

mCount = 0
if lastM != -1:
for index in range(0, lastM + 1):
mCount += M[index]
if index != lastM:
mCount += travel[index]

pCount = 0
if lastP != -1:
for index in range(0, lastP + 1):
pCount += P[index]
if index != lastP:
pCount += travel[index]

gCount = 0
if lastG != -1:
for index in range(0, lastG + 1):
gCount += G[index]
if index != lastG:
gCount += travel[index]

return mCount + pCount + gCount


• class Solution {
public:
int garbageCollection(vector<string>& garbage, vector<int>& travel) {
int n = garbage.size(), m = travel.size();
int last[26]{};
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += garbage[i].size();
for (char& c : garbage[i]) {
last[c - 'A'] = i;
}
}
int s[m + 1];
s[0] = 0;
for (int i = 1; i <= m; ++i) {
s[i] = s[i - 1] + travel[i - 1];
}
for (int i : last) {
ans += s[i];
}
return ans;
}
};

• func garbageCollection(garbage []string, travel []int) (ans int) {
last := [26]int{}
for i, s := range garbage {
ans += len(s)
for _, c := range s {
last[c-'A'] = i
}
}
s := make([]int, len(travel)+1)
for i, x := range travel {
s[i+1] = s[i] + x
}
for _, i := range last {
ans += s[i]
}
return
}

• function garbageCollection(garbage: string[], travel: number[]): number {
const n = garbage.length;
const m = travel.length;
let ans = 0;
const last = new Array(26).fill(0);
for (let i = 0; i < n; ++i) {
ans += garbage[i].length;
for (const c of garbage[i]) {
last[c.charCodeAt(0) - 'A'.charCodeAt(0)] = i;
}
}
const s = new Array(m + 1).fill(0);
for (let i = 1; i <= m; ++i) {
s[i] = s[i - 1] + travel[i - 1];
}
for (const i of last) {
ans += s[i];
}
return ans;
}


• impl Solution {
pub fn garbage_collection(garbage: Vec<String>, travel: Vec<i32>) -> i32 {
let n = garbage.len();
let cs = [b'M', b'P', b'G'];
let mut count = [0, 0, 0];
for s in garbage.iter() {
for c in s.as_bytes().iter() {
count[if c == &b'M' {
0
} else if c == &b'P' {
1
} else {
2
}] += 1;
}
}

let mut res = 0;
for i in 0..3 {
for j in 0..n {
let s = &garbage[j];
for c in s.as_bytes().iter() {
if c == &cs[i] {
res += 1;
count[i] -= 1;
}
}
if count[i] == 0 {
break;
}

res += travel[j];
}
}
res
}
}


• public class Solution {
public int GarbageCollection(string[] garbage, int[] travel) {
int len = garbage.Length;
int res = 0;
HashSet<char> s = new HashSet<char>();
for (int i = len - 1; i >= 0; i--) {
foreach (char ch in garbage[i].ToCharArray()) {
if (!s.Contains(ch))
}
res += garbage[i].Length;
res += i > 0 ? s.Count * travel[i - 1] : 0;
}
return res;
}
}



Explain:

nope.

Complexity:

• Time complexity : O(n).
• Space complexity : O(n).