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.

Examples

Example 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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:

1
2
3
4
5
6
7
8
9
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.

Solution

Method 1 – Prefix Sum and Last Occurrence Tracking

Intuition

Each truck only needs to travel as far as the last house containing its garbage type. The total time is the sum of all garbage pickups plus the travel time for each truck up to its last relevant house. Prefix sums help quickly compute travel times.

Approach

  1. Count the total number of each garbage type (‘M’, ‘P’, ‘G’).
  2. For each type, find the last house index where it appears.
  3. Build a prefix sum array for travel times to quickly get the total travel time to any house.
  4. For each garbage type, add its total pickup time and the travel time to its last house.
  5. Sum the results for all types to get the answer.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    int garbageCollection(vector<string>& garbage, vector<int>& travel) {
        int n = garbage.size();
        vector<int> pre(n, 0);
        for (int i = 1; i < n; ++i) pre[i] = pre[i-1] + travel[i-1];
        int ans = 0, lastM = 0, lastP = 0, lastG = 0;
        for (int i = 0; i < n; ++i) {
            for (char c : garbage[i]) {
                ans++;
                if (c == 'M') lastM = i;
                if (c == 'P') lastP = i;
                if (c == 'G') lastG = i;
            }
        }
        ans += pre[lastM] + pre[lastP] + pre[lastG];
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func garbageCollection(garbage []string, travel []int) int {
    n := len(garbage)
    pre := make([]int, n)
    for i := 1; i < n; i++ {
        pre[i] = pre[i-1] + travel[i-1]
    }
    ans, lastM, lastP, lastG := 0, 0, 0, 0
    for i := 0; i < n; i++ {
        for _, c := range garbage[i] {
            ans++
            if c == 'M' {
                lastM = i
            }
            if c == 'P' {
                lastP = i
            }
            if c == 'G' {
                lastG = i
            }
        }
    }
    ans += pre[lastM] + pre[lastP] + pre[lastG]
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    public int garbageCollection(String[] garbage, int[] travel) {
        int n = garbage.length;
        int[] pre = new int[n];
        for (int i = 1; i < n; ++i) pre[i] = pre[i-1] + travel[i-1];
        int ans = 0, lastM = 0, lastP = 0, lastG = 0;
        for (int i = 0; i < n; ++i) {
            for (char c : garbage[i].toCharArray()) {
                ans++;
                if (c == 'M') lastM = i;
                if (c == 'P') lastP = i;
                if (c == 'G') lastG = i;
            }
        }
        ans += pre[lastM] + pre[lastP] + pre[lastG];
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    fun garbageCollection(garbage: Array<String>, travel: IntArray): Int {
        val n = garbage.size
        val pre = IntArray(n)
        for (i in 1 until n) pre[i] = pre[i-1] + travel[i-1]
        var ans = 0
        var lastM = 0
        var lastP = 0
        var lastG = 0
        for (i in 0 until n) {
            for (c in garbage[i]) {
                ans++
                if (c == 'M') lastM = i
                if (c == 'P') lastP = i
                if (c == 'G') lastG = i
            }
        }
        ans += pre[lastM] + pre[lastP] + pre[lastG]
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution:
    def garbageCollection(self, garbage: list[str], travel: list[int]) -> int:
        n: int = len(garbage)
        pre: list[int] = [0] * n
        for i in range(1, n):
            pre[i] = pre[i-1] + travel[i-1]
        ans: int = 0
        lastM: int = 0
        lastP: int = 0
        lastG: int = 0
        for i in range(n):
            for c in garbage[i]:
                ans += 1
                if c == 'M': lastM = i
                if c == 'P': lastP = i
                if c == 'G': lastG = i
        ans += pre[lastM] + pre[lastP] + pre[lastG]
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
impl Solution {
    pub fn garbage_collection(garbage: Vec<String>, travel: Vec<i32>) -> i32 {
        let n = garbage.len();
        let mut pre = vec![0; n];
        for i in 1..n {
            pre[i] = pre[i-1] + travel[i-1];
        }
        let (mut ans, mut last_m, mut last_p, mut last_g) = (0, 0, 0, 0);
        for (i, s) in garbage.iter().enumerate() {
            for c in s.chars() {
                ans += 1;
                if c == 'M' { last_m = i; }
                if c == 'P' { last_p = i; }
                if c == 'G' { last_g = i; }
            }
        }
        ans += pre[last_m] + pre[last_p] + pre[last_g];
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    garbageCollection(garbage: string[], travel: number[]): number {
        const n = garbage.length;
        const pre = Array(n).fill(0);
        for (let i = 1; i < n; ++i) pre[i] = pre[i-1] + travel[i-1];
        let ans = 0, lastM = 0, lastP = 0, lastG = 0;
        for (let i = 0; i < n; ++i) {
            for (const c of garbage[i]) {
                ans++;
                if (c === 'M') lastM = i;
                if (c === 'P') lastP = i;
                if (c === 'G') lastG = i;
            }
        }
        ans += pre[lastM] + pre[lastP] + pre[lastG];
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n + m) — n is the number of houses, m is the total number of garbage units. We scan all houses and garbage units once.
  • 🧺 Space complexity: O(n) — We use a prefix sum array of size n for travel times.