↓Skip to main content
  1. LeetCode/

LeetCode 2391: Minimum Amount of Time to Collect Garbage

·2 mins· ·
LeetCode Medium Prefix-Sum String Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-15

  • Submissions: Accepted ×2

Approach
#

Only one truck moves at a time, so the total is the sum of every truck’s time, in two parts:

  • Pickup: 1 minute per unit, which is the total length of all strings.
  • Driving: each truck only drives to the house where its type last appears, with the distance from a prefix sum of travel (Prefix Sum Template 4).

My Solution
#

I originally accumulated “unpaid travel” per truck while scanning and added it when the truck’s type appeared; the second version moved the pickup time into one final sum. Rewritten as “last occurrence + prefix sum”:

class Solution:
    def garbageCollection(self, garbage: List[str], travel: List[int]) -> int:
        prefix = [0]
        for d in travel:
            prefix.append(prefix[-1] + d)
        last = {}
        for i, g in enumerate(garbage):
            for t in set(g):
                last[t] = i
        return sum(map(len, garbage)) + sum(prefix[i] for i in last.values())

Complexity
#

Time O(total characters + n), space O(n).

Mistakes to Avoid
#

  • A type that never appears means that truck never leaves.
  • A truck stops at the last house with its type, not the last house overall.

Interview Explanation
#

Total time is pickup plus driving. Pickup is the number of garbage units; each truck drives only to the last occurrence of its type, looked up in a prefix sum of travel. Time O(total characters).

Related

LeetCode 3494: Find the Minimum Amount of Time to Brew Potions
·2 mins
LeetCode Medium Simulation Prefix-Sum Array
LeetCode 3494 note: for each potion, push forward to get the last wizard’s finish time (everyone must be free), then subtract backward to get each wizard’s finish time
LeetCode 3070: Count Submatrices with Top-Left Element and Sum Less Than k
·2 mins
LeetCode Medium Array Matrix Prefix-Sum
LeetCode 3070 note: submatrices containing the top-left corner are exactly the 2D prefix sums P[i][j]; count those <= k
LeetCode 238: Product of Array Except Self
·2 mins
LeetCode Medium Array Prefix-Sum
LeetCode 238 note: each answer is the product of everything to the left times everything to the right; two passes, no division
LeetCode 79: Word Search
·3 mins
LeetCode Medium Array String Backtracking Depth-First-Search
LeetCode 79 note: backtrack from every cell, matching the next character in four directions with visited cells marked temporarily and restored; prune early with character counts
LeetCode 49: Group Anagrams
·2 mins
LeetCode Medium Array Hash-Table String Sorting
LeetCode 49 note: use each string’s sorted form as a key; strings sharing a key form one anagram group
LeetCode 2410: Maximum Matching of Players With Trainers
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 2410 note: sort both sides, then same-direction pointers match the weakest player with the smallest trainer that is good enough