[프로그래머스/Level2] 카펫(Python)

SeokHyun·2022년 7월 17일
0

프로그래머스

목록 보기
25/32

문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/42842

문제 접근

해당 문제는 사각형 면적 == brown+yellow인 것을 활용한 문제이다.

  1. brown+yellow의 약수를 구한다.
  2. 여기서, widthheight보다 커질 때를 찾는다.
  3. (핵심) (width-2)*(height-2) == yellow인지를 검사해야한다.
    • 이유: 갈색은 노란색을 중심으로 위아래, 양옆으로 1줄씩 있기 때문

소스 코드

from typing import List

def solution(brown: int, yellow: int) -> List[int]:
    answer = []
    total = brown + yellow
    total_divisor = getDivisor(total)
    
    for width in total_divisor:
        height = total // width
        yellow_size = (width - 2) * (height - 2)
        if width >= height and yellow_size == yellow:
            answer = [width, height]
            break

    return answer


def getDivisor(n: int) -> List[int]:
    divisor = []
    divisor_pair = []
    
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            divisor.append(i)
            if i != (n // i):
                divisor_pair.append(n // i)

    return divisor + divisor_pair[::-1]
profile
SI를 넘어 백엔드 엔지니어를 향하여

0개의 댓글