문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/42842
해당 문제는 사각형 면적 == brown+yellow
인 것을 활용한 문제이다.
brown+yellow
의 약수를 구한다.width
가 height
보다 커질 때를 찾는다.(width-2)*(height-2) == yellow
인지를 검사해야한다.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]