<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>&#39;M&#39;</code>, <code>&#39;P&#39;</code> and <code>&#39;G&#39;</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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> garbage = [&quot;G&quot;,&quot;P&quot;,&quot;GP&quot;,&quot;GG&quot;], travel = [2,4,3]
<strong>Output:</strong> 21
<strong>Explanation:</strong>
The paper garbage truck:
1. Travels from house 0 to house 1
2. Collects the paper garbage at house 1
3. Travels from house 1 to house 2
4. Collects the paper garbage at house 2
Altogether, it takes 8 minutes to pick up all the paper garbage.
The glass garbage truck:
1. Collects the glass garbage at house 0
2. Travels from house 0 to house 1
3. Travels from house 1 to house 2
4. Collects the glass garbage at house 2
5. Travels from house 2 to house 3
6. Collects the glass garbage at house 3
Altogether, it takes 13 minutes to pick up all the glass garbage.
Since there is no metal garbage, we do not need to consider the metal garbage truck.
Therefore, it takes a total of 8 + 13 = 21 minutes to collect all the garbage.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> garbage = [&quot;MMM&quot;,&quot;PGM&quot;,&quot;GP&quot;], travel = [3,10]
<strong>Output:</strong> 37
<strong>Explanation:</strong>
The metal garbage truck takes 7 minutes to pick up all the metal garbage.
The paper garbage truck takes 15 minutes to pick up all the paper garbage.
The glass garbage truck takes 15 minutes to pick up all the glass garbage.
It takes a total of 7 + 15 + 15 = 37 minutes to collect all the garbage.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= garbage.length &lt;= 10<sup>5</sup></code></li>
	<li><code>garbage[i]</code> consists of only the letters <code>&#39;M&#39;</code>, <code>&#39;P&#39;</code>, and <code>&#39;G&#39;</code>.</li>
	<li><code>1 &lt;= garbage[i].length &lt;= 10</code></li>
	<li><code>travel.length == garbage.length - 1</code></li>
	<li><code>1 &lt;= travel[i] &lt;= 100</code></li>
</ul>

from collections import Counter
class Solution:
    def garbageCollection(self, garbage: List[str], travel: List[int]) -> int:
        """
        [Observation]
        since only one truck can be working at one time, there is no optimization to run the truck concurrently. So we should run the truck one by one
        
        [Intuition]
        if we pick up the last one of the same kind of garbage, then we do not need to go to the next house
        maybe it is helpful to know when the last house of each kind of garbage is
        
        [Implementation/greedy/brute force]
        initialize total time
        
        create a time_dic that tracks the time spent traveling from the last house with each type of garbage
        
        loop through each house
            use Counter to count number of each garbage
            for each type of garbage here (hardcode 'M', 'P', 'G'):
                add time to each key in time_dic to res
                if that garbage type is in counter
                    add collecting time (count of the garbage type) to res
                    add the traveling time from the time_dic for that garbage
                    reset the time_dic of that garbage to 0
        
        return res
        
        """
        
        # initialize total time
        res = 0
        
        # create a time_dic that tracks the time spent traveling from the last house with each type of garbage
        time_dic = dict({'P': 0, 'M': 0, 'G': 0})
        travel = [0]+travel
        
        # loop through each house
        for i in range(len(garbage)):
            # use Counter to count number of each garbage
            counter = Counter(garbage[i])
            # for each type of garbage here (hardcode 'M', 'P', 'G'):
            for g in ['M', 'P', 'G']:
                # add time to each key in time_dic
                time_dic[g] = time_dic[g]+travel[i]
                # if that garbage type is in counter
                if g in counter:
                    # add collecting time (count of the garbage type) to res
                    res +=counter[g]
                    # add the traveling time from the time_dic for that garbage to res
                    res +=time_dic[g]
                    # reset the time_dic of that garbage to 0
                    time_dic[g] = 0
        
        return res
1084