Leetcode 2391: Minimum Amount of Time to Collect Garbage

grid47
grid47
Exploring patterns and algorithms
Mar 12, 2024 7 min read

You are given an array of strings garbage where each string represents the garbage collected at a specific house. The characters ‘M’, ‘P’, and ‘G’ represent metal, paper, and glass garbage, respectively. The time to collect one unit of garbage is 1 minute. You are also given an array travel where each element represents the time required to travel between two consecutive houses. Three trucks are responsible for picking up different types of garbage and can only travel sequentially from house 0 to the end. Each truck only collects one type of garbage and each truck can only be used one at a time. Return the minimum time required to pick up all the garbage.
Problem
Approach
Steps
Complexity
Input: The input consists of two arrays: `garbage` and `travel`. `garbage[i]` represents the garbage at house `i` and consists of the characters 'M', 'P', and 'G'. `travel[i]` is the time it takes to travel between house `i` and house `i + 1`.
Example: garbage = ['G','P','GP','GG'], travel = [2,4,3]
Constraints:
• 2 <= garbage.length <= 10^5
• garbage[i] consists of the characters 'M', 'P', 'G'.
• 1 <= garbage[i].length <= 10
• travel.length == garbage.length - 1
• 1 <= travel[i] <= 100
Output: Return the total minimum time needed to collect all the garbage. This includes both the time to pick up the garbage and the time spent traveling.
Example: Output: 21
Constraints:
Goal: The goal is to minimize the total time by collecting the garbage for each truck and considering the travel time for each truck's route.
Steps:
• 1. Iterate through each house and for each type of garbage, track the last house where it appears.
• 2. Calculate the time required to collect all the garbage of each type.
• 3. Add the travel time for each garbage truck based on the last house where its garbage is located.
Goal: The input sizes are large and the problem must be solved efficiently.
Steps:
• Ensure the solution handles up to 100,000 houses efficiently.
Assumptions:
• Each truck must pick up all the garbage of its type.
• There are always at least one type of garbage to collect.
Input: garbage = ['G','P','GP','GG'], travel = [2,4,3]
Explanation: The metal truck collects no garbage, so we ignore it. The paper truck collects garbage at house 1 and 2, traveling a total of 6 minutes. The glass truck collects garbage from houses 0, 2, and 3, traveling a total of 13 minutes. The total time is 6 + 13 = 21 minutes.

Input: garbage = ['MMM','PGM','GP'], travel = [3,10]
Explanation: The metal truck collects garbage from houses 0, 1, and 2, traveling a total of 7 minutes. The paper truck collects garbage from houses 0, 1, and 2, traveling 15 minutes. The glass truck collects garbage from houses 0, 1, and 2, also traveling 15 minutes. The total time is 7 + 15 + 15 = 37 minutes.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus