Minimum Amount of Time to Collect Garbage

You are given a 0-indexed array of strings garbage where garbage[i] represents the assortment of garbage at the i^th 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.

Example 1
Inputgarbage = ["G","P","GP","GG"], travel = [2,4,3]
Output21
The paper truck takes 8 minutes and the glass truck takes 13 minutes, while no metal truck is needed, for a total of 21 minutes.
Example 2
Inputgarbage = ["MMM","PGM","GP"], travel = [3,10]
Output37
The metal truck takes 7 minutes, the paper truck takes 15 minutes, and the glass truck takes 15 minutes, for a total of 37 minutes.

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

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate