Minimum Amount of Time to Collect Garbage - Problem

Imagine you're managing a smart city's waste collection system! ๐Ÿš› You have three specialized garbage trucks - one for Metal (M), one for Paper (P), and one for Glass (G).

Given an array garbage where garbage[i] represents the types of garbage at house i (containing only 'M', 'P', 'G' characters), and an array travel where travel[i] is the minutes needed to travel from house i to house i+1.

Rules:

  • Each truck starts at house 0 and visits houses in order
  • Picking up 1 unit of garbage takes 1 minute
  • Only one truck can operate at a time
  • Trucks don't need to visit every house (only where their garbage type exists)

Goal: Return the minimum total time needed to collect all garbage from all houses.

Input & Output

basic_case.py โ€” Python
$ Input: garbage = ["G","P","GP","GG"], travel = [2,4,3]
โ€บ Output: 21
๐Ÿ’ก Note: Metal truck: no metal garbage, 0 time. Paper truck: travels 0->1->2 (2+4=6 minutes) + picks up 2 paper (2 minutes) = 8 minutes. Glass truck: travels 0->1->2->3 (2+4+3=9 minutes) + picks up 4 glass (4 minutes) = 13 minutes. Total: 0 + 8 + 13 = 21
all_types.py โ€” Python
$ Input: garbage = ["MMM","PGM","GP"], travel = [3,10]
โ€บ Output: 37
๐Ÿ’ก Note: Metal truck: travels to house 1 (3 minutes) + picks up 4 metal (4 minutes) = 7 minutes. Paper truck: travels to house 2 (3+10=13 minutes) + picks up 2 paper (2 minutes) = 15 minutes. Glass truck: travels to house 2 (3+10=13 minutes) + picks up 2 glass (2 minutes) = 15 minutes. Total: 7 + 15 + 15 = 37
single_house.py โ€” Python
$ Input: garbage = ["MPG"], travel = []
โ€บ Output: 3
๐Ÿ’ก Note: All trucks start at house 0, no travel needed. Each picks up 1 unit: Metal=1, Paper=1, Glass=1. Total: 1 + 1 + 1 = 3

Constraints

  • 1 โ‰ค garbage.length โ‰ค 105
  • garbage[i] consists only of the characters 'M', 'P', and 'G'
  • 1 โ‰ค garbage[i].length โ‰ค 10
  • travel.length == garbage.length - 1
  • 1 โ‰ค travel[i] โ‰ค 100
  • Total garbage units โ‰ค 106

Visualization

Tap to expand
Smart City Garbage Collection SystemMPGHouse 0PGHouse 1MHouse 2PHouse 32 min4 min3 minMMetal Route: 0โ†’2 (6 min total)PPaper Route: 0โ†’3 (11 min total)GGlass Route: 0โ†’1 (4 min total)Time Calculation๐Ÿš› Metal Truck:Last stop: House 2 | Travel: 6 min | Pickup: 1 min | Total: 7 min๐Ÿš› Paper Truck:Last stop: House 3 | Travel: 9 min | Pickup: 2 min | Total: 11 min๐Ÿš› Glass Truck:Last stop: House 1 | Travel: 2 min | Pickup: 2 min | Total: 4 minFinal Answer7 + 11 + 4 = 22Total Minutes
Understanding the Visualization
1
Route Planning
Scan all houses to determine the farthest each truck needs to travel
2
Garbage Collection
Count total pickup time for each garbage type (M, P, G)
3
Travel Optimization
Calculate minimum travel time to each truck's final destination
4
Total Coordination
Sum pickup + travel time since trucks operate sequentially
Key Takeaway
๐ŸŽฏ Key Insight: Each truck only needs to travel to the last house containing their garbage type, optimizing the total collection time through smart route planning.
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 5
58.9K Views
Medium Frequency
~12 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen