Skip to content

Practice Minimum Amount of Time to Collect Garbage

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2391
LeetCode Problem

Minimum Amount of Time to Collect Garbage

Problem Statement

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.

Source: Minimum Amount of Time to Collect Garbage on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
garbage = ["G","P","GP","GG"], travel = [2,4,3]
Output
21
Explanation

The glass truck collects four units and must reach house 3, spending 2 + 4 + 3 = 9 minutes travelling and 4 collecting: 13 minutes. The paper truck collects two units and stops at house 2, costing 2 + 4 + 2 = 8 minutes. There is no metal, so its truck need not move. Only one truck can operate at a time, making the total 13 + 8 = 21. Do not send every truck to the last house or add return journeys that the task does not require.

# Example 2

Input
garbage = ["MMM","PGM","GP"], travel = [3,10]
Output
37
Explanation

There are eight units to collect. Metal appears last at house 1, so its truck spends 3 minutes travelling and 4 collecting, for 7. Paper and glass each appear last at house 2; each truck spends 3 + 10 = 13 minutes travelling and 2 collecting, for 15 apiece. The minimum is 7 + 15 + 15 = 37. Travel is charged separately for each truck crossing a road, even though the road is shared; using only the largest travel time would assume forbidden simultaneous work.

Constraints

2 <= garbage.length <= 10^5
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

How to solve Minimum Amount of Time to Collect Garbage

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Pickup costs one minute per unit no matter which truck collects it, so only travel is negotiable, and each truck should drive exactly to the last house containing its type. The answer is the total count of garbage characters plus, per type, the prefix sum of travel to that type's last house.

Why brute force is not enough

Simulating each truck and re-summing travel for every house it reaches is O(n^2) when done carelessly, about 10^10 additions for garbage.length up to 10^5; sending every truck to the last house is simpler but overcounts travel for types that end early.

Approach

  1. Add the lengths of all garbage strings; this pickup time is unavoidable because only one truck works at a time.
  2. Scan the houses once, recording for M, P and G the index of the last house containing that letter. A type that never appears has no last house and its truck never leaves house 0.
  3. Maintain a running prefix of travel so the cost of reaching house i, the sum of travel[0] through travel[i-1], is available in constant time.
  4. For each type that appears, add the prefix cost of its last house. Trucks never return and stop at their last relevant house, so nothing else is charged.

Walkthrough of Example 1

garbage = ["G","P","GP","GG"], travel = [2,4,3] → 21

  1. Pickup time is the total character count 1 + 1 + 2 + 2 = 6.
  2. G last appears at house 3, P at house 2, and M never appears.
  3. Prefix travel: house 1 costs 2, house 2 costs 6, house 3 costs 9.
  4. Travel is 9 for glass, 6 for paper, 0 for metal.
  5. Total 6 + 9 + 6 + 0 = 21.

Time and space

Time O(n + L) · Space O(1). With n houses and L total characters across garbage, each is scanned once; a running prefix and three last-house indices suffice.

Interview pitfalls

  • Sending every truck to the final house, overcharging travel for types whose last occurrence is earlier.
  • Adding return trips, or charging a shared road once because trucks share it; each truck pays its own travel.
  • Using the maximum of the three trucks' times instead of their sum, which assumes forbidden parallel work.
  • Off-by-one in the prefix: travel[i] is the road from house i to house i + 1, so reaching house k sums travel[0] through travel[k-1].
  • Mishandling a type that appears only at house 0 or not at all; its truck contributes zero travel.

Follow-up questions interviewers ask

  • How does the answer change if trucks must return to house 0 after finishing?
  • If the three trucks could run in parallel, why does the answer become a maximum rather than a sum?
  • How would you model a capacity limit that forces trucks to return and empty periodically?

Further reading on the technique: Prefix sum

Related LeetCode Problems

Practice more problems similar to Minimum Amount of Time to Collect Garbage.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.