Ctrl K

Recycling Truck Minutes

Medium
Houses along a street are numbered from 0 to n − 1. House i has put out bags described by the string bins[i], where each letter is one bag: M for metal, P for paper, G for glass. An empty string means the house has nothing out. Loading one bag takes 1 minute. Driving from house i to house i + 1 takes drive[i] minutes. Each kind of material has its own truck. Every truck starts at house 0 and only moves forward, house by house; a truck never needs to go past the last house holding its material, and if there is none of that material at all, it stays put. Only one truck works at a time: while one is driving or loading, the others wait. Return the total number of minutes needed to collect everything.
Example 1
bins=['PM', 'G', 'P'], drive=[5, 2] → 16

Metal: 1 minute to load at house 0. Glass: 5 minutes of driving plus 1 to load. Paper: 1 + 5 + 2 + 1 = 9 minutes. Total 1 + 6 + 9 = 16.

Example 2
bins=['GGG', 'MM', 'MG', 'G'], drive=[4, 1, 3] → 21

Glass drives all the way to house 3: 8 minutes of road and 5 units — 13. Metal stops at house 2: 5 minutes of road and 3 units — 8. There is no paper. Total 21.

Example 3
bins=['MPG'], drive=[] → 3

There is only one house, so no driving: each of the three trucks spends one minute loading.

Constraints
  • 1 ≤ bins.length ≤ 10⁵
  • 0 ≤ bins[i].length ≤ 10
  • bins[i] consists only of the letters M, P and G
  • drive.length = bins.length − 1
  • 1 ≤ drive[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRoll Letters by Prefix MediumPalindrome After a Few Fixes MediumGather the Balls in Each Box MediumBooks Between Bookends MediumRoll Letters by Ranges Medium
loading Python…
Input
bins=['PM', 'G', 'P'], drive=[5, 2]
Expected
16

Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.