카펫

최민수·2023년 5월 15일
0

알고리즘

목록 보기
55/94
import math

def solution(brown, yellow):
    total_size = brown+yellow
    
    for width in range(total_size//2, math.floor(math.sqrt(total_size))-1, -1):
        if total_size % width == 0:
            length = int(total_size / width)
            if (width-2) * (length-2) == yellow:
                return [width, length]

문제에서 total_size가 소수이거나 가로 세로 개수와 yellow 의 개수가 맞지 않는 상황의 인풋은 주어지지 않음을 알 수 있으므로 간단하게 로직을 작성할 수 있다.

세로 길이보다 가로가 길기 때문에 역순으로 따져 내려오면서 답을 구해주면 된다. 무난한 문제이다.


문제 출처: https://school.programmers.co.kr/learn/courses/30/lessons/42842

profile
CS, 개발 공부기록 🌱

0개의 댓글