https://school.programmers.co.kr/learn/courses/30/lessons/42842
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
def solution(brown, yellow):
total = brown + yellow
for height in range(3, int(total ** 0.5) + 1): # 최소 높이 3부터 √total까지 탐색
if total % height == 0:
width = total // height
if (width - 2) * (height - 2) == yellow:
return [width, height]
이차방정식 반복문 풀이를 하려고 했는데 계속 시간초과났다...
약수 쌍은 항상 제곱근을 기준으로 대칭적으로 분포하는 것을 이용해 반복문 범위 대폭 줄여주기!!
'프로그래머스' 카테고리의 다른 글
프로그래머스 150368. 이모티콘 할인행사 (0) | 2024.11.24 |
---|---|
프로그래머스 42839. 소수 찾기 (0) | 2024.11.19 |
프로그래머스 42840. 모의고사 (0) | 2024.11.16 |
프로그래머스 77486. 다단계 칫솔 판매 (0) | 2024.11.05 |
프로그래머스 84512. 모음사전 (0) | 2024.11.04 |