Make Unreal REAL.
article thumbnail
Level 2. 롤케이크 자르기

 

 

토핑의 번호가 특별한 의미를 갖지 않기 때문에 map 대신 unordered_map을 사용했다.

  • C++의 맵 자료구조는 한 번 접근한 키는 값이 0이어도 size에 포함되기 때문에, erase를 해주어야 한다.

 

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

int solution(vector<int> topping)
{
    int answer = 0;
    unordered_map<int, int> umapA, umapB;

    for (int tp : topping)
        ++umapB[tp];

    for (int tp : topping)
    {
        ++umapA[tp];

        if (--umapB[tp] <= 0)
            umapB.erase(tp);

        if (umapA.size() == umapB.size())
            ++answer;
    }

    return answer;
}

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

Level 2. 호텔 대실  (0) 2023.08.04
Level 3. 징검다리 건너기  (0) 2023.08.03
Level 3. 불량 사용자  (0) 2023.08.01
Level 3. 여행경로  (0) 2023.07.31
Level 3. 가장 먼 노드  (0) 2023.07.30
profile

Make Unreal REAL.

@diesuki4

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

검색 태그