본문 바로가기

알고리즘

[완전탐색] 프로그래머스 - 카펫

python

from math import ceil, floor, sqrt


def solution(brown, yellow):
    answer = []
    
    height = 0
    width = 0
    area = brown + yellow

    sqrt_area = (ceil(sqrt(area)))
    
    for i in range(3, sqrt_area+1):
        if area % i == 0:
            if (area/i - 2)*(i-2) == yellow:
                height = i
                width = area/i
    
    answer.append(int(max(height, width)))
    answer.append(int(min(height, width)))
    return answer