본문 바로가기
프로그래머스/Lv.1

[C++] 프로그래머스 덧칠하기

by MINU.SHINNNN 2023. 11. 4.

https://school.programmers.co.kr/learn/courses/30/lessons/161989

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

풀이

unordered_map 자료구조를 사용해 색칠한 벽을 체크할 변수 um을 선언합니다.

section을 순회하며 색칠하지 않은 벽일 경우 m개 만큼 색칠합니다.

색칠할 경우 answer++를 해주면 정답을 리턴할 수 있습니다. 

#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

int solution(int n, int m, vector<int> section) {
    int answer = 0;
    unordered_map<int, int> um;
    vector<int> wall(n);
    
    for (auto i : section) {
        /* 
            색칠한게 아닌 경우 색칠
        */
        if (!um[i]) {
            for (int j = 0; j<m; j++) {
                um[i+j] = 1;
            }
            answer++;
        }
    }
    
    return answer;
}