[프로그래머스 / C++] 카펫

Taegang Yun·2023년 9월 8일
1

문제 바로가기

lv2

💡 아이디어

노랑이가 들어가야 되니까..가로의 최소는 무조건 2 이상이여야 하고
노랑이 개수는 가로 - 2 * 세로 - 2 와 같다는 것을 발견해서 이걸 이용했다.
가로 세로를 경우의 수를 따져서 푸는 방법이다.

🖥️ 소스코드


#include <string>
#include <vector>

using namespace std;

int tmp_width, tmp_height;

vector<int> solution(int brown, int yellow) {
    vector<int> answer;
    int halfbrown = brown / 2;
    int mx_width = halfbrown % 2 == 0 ? halfbrown / 2 : (halfbrown + 1) / 2;
    
    for(int i = 2; i <= mx_width ; i++)
    {
        tmp_width = halfbrown - i + 1;
        tmp_height = i + 1;
        if(tmp_width >= tmp_height && ((tmp_width - 2) * (tmp_height - 2) == yellow))
        {
            answer.push_back(tmp_width);
            answer.push_back(tmp_height);
        }
    }
    return answer;
}
profile
언젠간 전문가가 되겠지

0개의 댓글

관련 채용 정보