Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

그냥하는코딩

프로그래머스 3단계 - [행렬 테두리 회전하기] 본문

부스트캠프 준비기/코딩테스트 알고리즘

프로그래머스 3단계 - [행렬 테두리 회전하기]

UKkim 2021. 6. 23. 20:49

https://programmers.co.kr/learn/courses/30/lessons/77485

 

코딩테스트 연습 - 행렬 테두리 회전하기

6 6 [[2,2,5,4],[3,3,6,6],[5,1,6,3]] [8, 10, 25] 3 3 [[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]] [1, 1, 5, 3]

programmers.co.kr

1. 문제이해

- 이차원 배열 회전 문제

 

2. 코드 작성

def solution(rows, columns, queries):
    answer = []
    matrix = []
    
    for i in range(rows):
        matrix.append([j+(columns*i)+1 for j in range(columns)])
    
    for query in queries:
        rotate = get_rotate_list(matrix, query)
        answer.append(min([min(m) for m in rotate]))
        for i in range(len(rotate[0]) - 1):
            matrix[query[0]-1][query[1]+i] = rotate[0][i]
            matrix[query[2]-1][query[1]+i-1] = rotate[2][i+1]
        for i in range(len(rotate[1]) - 1):
            matrix[query[0]+i][query[3]-1] = rotate[1][i]
            matrix[query[0]+i-1][query[1]-1] = rotate[3][i+1]
    
    return answer

def get_rotate_list(matrix, query):
    # x1, y1, x2, y2 => query = [x1, y1, x2, y2]
    temp_list = []
    temp_list.append(matrix[query[0]-1][query[1]-1:query[3]])               # top (x1, y1) ~ (x1, y2)
    temp_list.append([m[query[3]-1] for m in matrix][query[0]-1:query[2]])  # right (x1, y2) ~ (x2, y2)
    temp_list.append(matrix[query[2]-1][query[1]-1:query[3]])               # bottom (x2, y1) ~ (x2, y2)
    temp_list.append([m[query[1]-1] for m in matrix][query[0]-1:query[2]])  # left (x1, y2) ~ (x2, y2)

    return temp_list

 

3. 풀이간 체크사항

 1) 범위를 잡아 각각에 넣어주는 부분을 신경써서 코딩할 것

 2) 이차원 배열을 코딩시에 범위를 벗어나지 않도록 유의할 것

Comments