def get_rotation_candidates(mat, query):
x1, y1, x2, y2 = query
# (x, y, val)
first = [(x1-1, y1 -1 + i, mat[x1-1][y1-1+i]) for i in range(y2-y1)]
second = [(x1-1+i, y2-1, mat[x1-1+i][y2-1]) for i in range(x2-x1)]
third = [(x2-1, y2-1-i, mat[x2-1][y2-1-i]) for i in range(y2-y1)]
fourth = [(x2-1-i,y1-1, mat[x2-1-i][y1-1]) for i in range(x2-x1)]
return first, second, third, fourth
def get_min(first, second, third, fourth):
return min(
min(first, key=lambda x: x[2])[2],
min(second, key=lambda x: x[2])[2],
min(third, key=lambda x: x[2])[2],
min(fourth, key=lambda x: x[2])[2],
)
def rotate(mat, first, second, third, fourth):
mat[first[0][0]][first[0][1]] = fourth[-1][-1]
mat[second[0][0]][second[0][1]] = first[-1][-1]
mat[third[0][0]][third[0][1]] = second[-1][-1]
mat[fourth[0][0]][fourth[0][1]] = third[-1][-1]
if len(first) >= 2:
for i in range(1, len(first)):
new_val = first[i-1][-1]
mat[first[i][0]][first[i][1]] = new_val
if len(second) >= 2:
for i in range(1, len(second)):
new_val = second[i-1][-1]
mat[second[i][0]][second[i][1]] = new_val
if len(third) >= 2:
for i in range(1, len(third)):
new_val = third[i-1][-1]
mat[third[i][0]][third[i][1]] = new_val
if len(fourth) >= 2:
for i in range(1, len(fourth)):
new_val = fourth[i-1][-1]
mat[fourth[i][0]][fourth[i][1]] = new_val
return mat
def solution(rows, columns, queries):
init_mat = [[i for i in range(1 + (j-1)*columns, 1 + j * columns)] for j in range(1, 1 + rows)]
mins = []
for query in queries:
first, second, third, fourth = get_rotation_candidates(init_mat, query)
mins.append(get_min(first, second, third, fourth))
init_mat = rotate(init_mat, first, second, third, fourth)
return mins
- 포인트
min(listOfTuples, key=lambda x: x[2])[2]
을 통해 third element 최솟값 얻기
- 테두리를 4분할해서 처리, 각 분할마다 첫점과 그 외를 나눠 해결