Make Unreal REAL.
article thumbnail
Level 2. 택배상자

 

 

스택을 활용한 간단한 시뮬레이션 문제다.

 

그냥 매번 보조 벨트에 넣고, 뺄 때는 보조 벨트에서만 빼면 된다.

 

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

int solution(vector<int> order)
{
    int answer = 0;
    size_t n = order.size();
    stack<int> stck;

    for (int i = 1; i <= n; ++i)
    {
        stck.emplace(i);

        while (!stck.empty() && stck.top() == order[answer])
        {
            ++answer;
            stck.pop();
        }
    }

    return answer;
}

'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글

Level 3. 단속카메라  (0) 2023.08.14
Level 3. 입국심사  (0) 2023.08.13
Level 3. 합승 택시 요금  (0) 2023.08.11
Level 2. 카카오프렌즈 컬러링북  (0) 2023.08.10
Level 3. 기지국 설치  (0) 2023.08.09
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그