본문 바로가기
코딩테스트

C++ ] leetCode 2391 - Minimum Amount of Time to Collect Garbage

by eteo 2023. 3. 25.

 

 

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

Input: garbage = ["G","P","GP","GG"], travel = [2,4,3]
Output: 21
Explanation:
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.

Example 2:

Input: garbage = ["MMM","PGM","GP"], travel = [3,10]
Output: 37
Explanation:
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.

 

Constraints:

  • 2 <= garbage.length <= 105
  • 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

 

 

 

 

 

 

 

 

class Solution {
public:
    int garbageCollection(vector<string>& garbage, vector<int>& travel) {
        int metal = 0, paper = 0, glass = 0;
        int lastMetal = 0, lastPaper = 0, lastGlass = 0;

        int idx = 0;
        for(auto& s : garbage)
        {
            for(char& c : s)
            {
                if(c == 'M')
                {
                    metal++;
                    lastMetal = idx;                        
                }                 
                else if(c == 'P')
                {
                    paper++;
                    lastPaper = idx;    
                }
                else if(c == 'G')
                {
                    glass++;
                    lastGlass = idx;
                }
            }
            idx++;
        }

        int metalTime = metal;
        int paperTime = paper;
        int glassTime = glass;

        metalTime += accumulate(travel.begin(), travel.begin() + lastMetal, 0);
        paperTime += accumulate(travel.begin(), travel.begin() + lastPaper, 0);
        glassTime += accumulate(travel.begin(), travel.begin() + lastGlass, 0);

        return metalTime + paperTime + glassTime;
    }
};

 

반복문으로 metal, paper, glass 개수를 구한다.

그리고 해당 garbage 종류를 찾은 마지막 인덱스도 기억해둔다.

각각 수거시간(1) 더해준 다음에, 1개이상 있는경우 마지막 집까지 찾아가는 시간을 누적합해준다.

 

 

✔ accumulate 함수는 배열에서 특정 구간의 원소들의 합을 구하는 데 쓰인다. 사용하려면 <algorithm> 헤더를 포함해줘야 한다.

첫번째 매개변수는 시작지점을 가리키는 iterator이고, 세번째 매개변수는 더할 때의 초기 시작값이다.

두번째 매개변수인데 마지막 요소 다음 위치를 가리키는 iterator를 인수로 받기 때문에 인덱스+1을 해주어야 한다.

 

int sum = accumulate(begin(arr), begin(arr) + idx + 1, 0);

예를들어 요소가 5개인 벡터가 있고, 인덱스 0을 제외하고 인덱스 1부터 인덱스 4까지의 합을 구하려면 아래처럼 해야한다.

vector<int> arr = {1, 2, 3, 4, 5};
int sum = accumulate(begin(arr) + 1, begin(arr) + 5, 0);
// ...
Result : 14