알고리즘
*프로그래머스)카펫-python
1일1공부실천하자
2023. 2. 5. 21:28
https://school.programmers.co.kr/learn/courses/30/lessons/42842
def solution(brown, yellow):
answer = []
total = brown + yellow
for width in range(1,total):
if total % width == 0:
height = total // width
if width >= height and 2*width + 2*height == brown + 4:
return [width,height]
return answer
처음엔 무슨 규칙으로 풀려 시도했었다.
즉 dp로 시도해보았다.
brown + yellow == width * height를 발견했다.
그러나 brown + yellow 의 약수들은 수없이 많았고 조건을 하나 더 추가해야만 했다.
어떤 조건을 걸어야 알맞은 width와 height를 잡을 수 있을까 고민했지만 결국 검색을 해보았다.
조건은 전체 width * 2 + heiight * 2 == brown + 4가 된다는 조건이었다.
여기서 width * 2 + heiight * 2가 어떻게 brown + 4가 되는지 잠깐 고민했었다.
해당 조건이 성립할 수 있는 것은 width와 height를 하나의 큰 yellow로 생각한면 yellow의 width와 height 만으로 brown의 수를 구할 수 있다.