데이터사이언스 기록기📚

[프로그래머스] Level 2_카펫(완전탐색) 본문

Coding Test/프로그래머스(Python)

[프로그래머스] Level 2_카펫(완전탐색)

syunze 2022. 9. 27. 18:03

📌문제 유형

완전탐색

 

📌문제

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

📌나의 문제풀이

1차 시도(38.5, 시간초과)
# 가로 길이 >= 세로 길이
# brown + yellow = return 곱

def solution(brown, yellow):
    answer = [2000000,0]
    total, row, column  = 0, 0, 0
    
    total = brown + yellow
    
    for i in range(total,1,-1):
        for j in range(1,total):
            if total == i * j and i-j < answer[0] - answer[1]  and i >= j:
                answer.append(i)
                answer.append(j)
                answer = answer[2:]

    return answer

 

2차 시도(76.9)
# 가로 길이 >= 세로 길이
# brown + yellow = return 곱

def solution(brown, yellow):
    answer = [2005000,0]
    total, row, column  = 0, 0, 0
    
    total = brown + yellow
    
    for i in range(total,1,-1):
        column = total // i 
        if column * i == total and i-column < answer[0] - answer[1]  and i >= column:
            answer.append(i)
            answer.append(column)
            answer = answer[2:]

    return answer

 

3차 시도(통과)

 - answer는 [가로, 세로]

  •  가로는 (노란색 격자 최댓값 + 갈색 격자 최댓값)으로 제한하여 가로가 더 크게 설정

 - total에 brown + yellow 값 대입

 - for문

  • i : 가로, column : 세로
  • i(가로)는column(세로)보다 커야하므로 if 조건문에 대입
  • (i - 2) * (column - 2) == yellow의 조건문을 대입하여 예외상황 없애기
def solution(brown, yellow):
    answer = [2005000,0]
    total, column  = 0, 0
    
    total = brown + yellow
    
    for i in range(total,1,-1):
        column = total // i 
        if column * i == total and i >= column and (i-2)*(column-2) == yellow:
            answer.append(i)
            answer.append(column)
            del answer[:2]

    return answer

 

📌리뷰

- 식의 관계를 파악하고 이해하기

728x90
Comments