[Programmers/프로그래머스] 2021 Dev-Matching: 웹 백엔드 개발자(상반기) 행렬 테두리 회전하기 - Python/파이썬 [해설/풀이]

SihoonCho·2022년 9월 29일
0
post-thumbnail
[Programmers/프로그래머스] 2021 Dev-Matching: 웹 백엔드 개발자(상반기) [코딩테스트]
  1. [Lv. 1] 로또의 최고 순위와 최저 순위
  2. [Lv. 2] 행렬 테두리 회전하기
  3. [Lv. 3] 다단계 칫솔 판매
  4. [Lv. 3] 헤비 유저가 소유한 장소

📌 문제


📝 제한사항


💻 입출력 예


📖 입출력 예 설명


📌 풀이


def rotate(x1, y1, x2, y2, graph):
    temp = graph[x1][y1]						# 좌상단 임시추출
    changed_values = [temp]
    for row in range(x1 + 1, x2 + 1):           # 좌측 테두리
        graph[row - 1][y1] = graph[row][y1]
        changed_values.append(graph[row][y1])

    for col in range(y1 + 1, y2 + 1):           # 하단 테두리
        graph[x2][col - 1] = graph[x2][col]
        changed_values.append(graph[x2][col])

    for row in range(x2 - 1, x1 - 1, -1):       # 우측 테두리
        graph[row + 1][y2] = graph[row][y2]
        changed_values.append(graph[row][y2])

    for col in range(y2 - 1, y1 - 1, -1):       # 상단 테두리
        graph[x1][col + 1] = graph[x1][col]
        changed_values.append(graph[x1][col])

    graph[x1][y1 + 1] = temp
    return min(changed_values)
    
    
def solution(rows, columns, queries):
    i = 0
    graph = [[0] * (columns + 1) for _ in range(rows + 1)]
    for row in range(1, rows + 1):
        for col in range(1, columns + 1):
            i += 1
            graph[row][col] = i
            
    answer = []
    for query in queries:
        x1, y1, x2, y2 = query
        min_val = rotate(x1, y1, x2, y2, graph)
        answer.append(min_val)
    
    return answer
profile
개발을 즐길 줄 아는 백엔드 개발자

0개의 댓글