2021 Dev-Matching - 웹 백앤드 개발자(상반기) - 행렬 테두리 회전하기 (Level 2)

 

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


Code

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
def solution_one(answer, query, board):
    v = lambda i, j: board[i][j]
    def s(i, j, v):
        board[i][j] = v

    min_r, min_c, max_r, max_c = map(lambda x: x - 1, query)
    nrows, ncols = (max_r - min_r)+1, (max_c - min_c)+1

    init_val = v(min_r, min_c)
    min_val = init_val
    for idxs in ([(min_r+r, min_c) for r in range(nrows)],
                 [(max_r, min_c+c) for c in range(ncols)],
                 [(max_r-r, max_c) for r in range(nrows)],
                 [(min_r, max_c-c) for c in range(ncols)]):
        for i in range(len(idxs)-1):
            val = v(*idxs[i+1])
            s(*idxs[i], val)
            min_val = val if val < min_val else min_val
    s(min_r, min_c+1, init_val)
    answer.append(min_val)
    return board

def solution(rows, columns, queries):
    answer = []
    board  = [[(i*columns + (j+1)) for j in range(columns)] for i in range(rows)]
    for query in queries:
        board = solution_one(answer, query, board)
    return answer

Complexity

$O(|\text{queries}| \times \text{rows} \times \text{columns})$