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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code