java.lang.Object
g2301_2400.s2391_minimum_amount_of_time_to_collect_garbage.Solution

public class Solution extends Object
2391 - Minimum Amount of Time to Collect Garbage.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> array of strings <code>garbage</code> where <code>garbage[i]</code> represents the assortment of garbage at the <code>i<sup>th</sup></code> house. <code>garbage[i]</code> consists only of the characters <code>'M'</code>, <code>'P'</code> and <code>'G'</code> representing one unit of metal, paper and glass garbage respectively. Picking up <strong>one</strong> unit of any type of garbage takes <code>1</code> minute.</p> <p>You are also given a <strong>0-indexed</strong> integer array <code>travel</code> where <code>travel[i]</code> is the number of minutes needed to go from house <code>i</code> to house <code>i + 1</code>.</p> <p>There are three garbage trucks in the city, each responsible for picking up one type of garbage. Each garbage truck starts at house <code>0</code> and must visit each house <strong>in order</strong>; however, they do <strong>not</strong> need to visit every house.</p> <p>Only <strong>one</strong> garbage truck may be used at any given moment. While one truck is driving or picking up garbage, the other two trucks <strong>cannot</strong> do anything.</p> <p>Return <em>the <strong>minimum</strong> number of minutes needed to pick up all the garbage.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> garbage = [&ldquo;G&rdquo;,&ldquo;P&rdquo;,&ldquo;GP&rdquo;,&ldquo;GG&rdquo;], travel = [2,4,3]</p> <p><strong>Output:</strong> 21</p> <p><strong>Explanation:</strong></p> <p>The paper garbage truck:</p> <ol> <li> <p>Travels from house 0 to house 1</p> </li> <li> <p>Collects the paper garbage at house 1</p> </li> <li> <p>Travels from house 1 to house 2</p> </li> <li> <p>Collects the paper garbage at house 2 Altogether, it takes 8 minutes to pick up all the paper garbage.</p> </li> </ol> <p>The glass garbage truck:</p> <ol> <li> <p>Collects the glass garbage at house 0</p> </li> <li> <p>Travels from house 0 to house 1</p> </li> <li> <p>Travels from house 1 to house 2</p> </li> <li> <p>Collects the glass garbage at house 2</p> </li> <li> <p>Travels from house 2 to house 3</p> </li> <li> <p>Collects the glass garbage at house 3</p> </li> </ol> <p>Altogether, it takes 13 minutes to pick up all the glass garbage.</p> <p>Since there is no metal garbage, we do not need to consider the metal garbage truck.</p> <p>Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> garbage = [&ldquo;MMM&rdquo;,&ldquo;PGM&rdquo;,&ldquo;GP&rdquo;], travel = [3,10]</p> <p><strong>Output:</strong> 37</p> <p><strong>Explanation:</strong></p> <p>The metal garbage truck takes 7 minutes to pick up all the metal garbage.</p> <p>The paper garbage truck takes 15 minutes to pick up all the paper garbage.</p> <p>The glass garbage truck takes 15 minutes to pick up all the glass garbage.</p> <p>It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= garbage.length <= 10<sup>5</sup></code></li> <li><code>garbage[i]</code> consists of only the letters <code>'M'</code>, <code>'P'</code>, and <code>'G'</code>.</li> <li><code>1 <= garbage[i].length <= 10</code></li> <li><code>travel.length == garbage.length - 1</code></li> <li><code>1 <= travel[i] <= 100</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • garbageCollection

      public int garbageCollection(String[] garbage, int[] travel)