Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Get
- 항해99
- json-server
- 프로그래머스
- 코딩테스트합격자되기
- redux
- 항해플러스
- C++
- 테코테코
- JavaScript
- sw expert academy
- maeil-mail
- java
- redux-saga
- 매일메일
- redux-toolkit
- axios
- 자바
- Algorithm
- useDispatch
- Python
- react-router
- 이코테
- react
- 알고리즘
- programmers
- react-redux
- createSlice
- SW
- 리액트
Archives
- Today
- Total
Binary Journey
[프로그래머스] 행렬 테두리 회전하기 본문
반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
머리로는 뭔가 할 수 있을 듯 말듯한데
결국 구글의 도움을 받아야 했음
def solution(rows, columns, queries):
answer = []
box = []
for i in range(rows):
row = []
for j in range(columns):
row.append(columns * i + j + 1)
box.append(row)
for querie in queries:
행1, 열1, 행2, 열2 = querie
행1 -= 1; 열1 -= 1; 행2 -= 1; 열2 -= 1
tmp = box[행1][열1]
small = tmp
# left
for i in range(행1 + 1, 행2 + 1):
box[i-1][열1] = box[i][열1]
small = min(small, box[i][열1])
# bottom
for i in range(열1 + 1, 열2 + 1):
box[행2][i-1] = box[행2][i]
small = min(small, box[행2][i])
# right
for i in range(행2 - 1, 행1 - 1, -1):
box[i+1][열2] = box[i][열2]
small = min(small, box[i][열2])
# top
for i in range(열2 - 1, 열1 - 1, -1):
box[행1][i+1] = box[행1][i]
small = min(small, box[행1][i])
box[행1][열1+1] = tmp
answer.append(small)
return answer
참고:https://latte-is-horse.tistory.com/146
더 좋은 코드를 발견해서 달아 놓음
def rotate(x1, y1, x2, y2, graph, answer):
# 오른쪽, 아래, 왼쪽, 위 순
dx = 0, 1, 0, -1
dy = 1, 0, -1, 0
d, x, y, tmp = 0, x1, y1, 10000
min_val = 10000
while d < 4:
nx = x + dx[d]
ny = y + dy[d]
min_val = min(min_val, graph[x][y])
# 범위 안인 경우
if x1 <= nx <= x2 and y1 <= ny <= y2:
graph[x][y], tmp = tmp, graph[x][y]
x, y = nx, ny
else:
d += 1
graph[x1][y1] = tmp
answer.append(min_val)
def solution(rows, columns, queries):
answer = []
graph = [[i + 1 for i in range(j * columns, (j + 1) * columns)] for j in range(rows)]
for query in queries:
x1, y1, x2, y2 = map(int, query)
rotate(x1 - 1, y1 - 1, x2 - 1, y2 - 1, graph, answer)
return answer
출처: https://intrepidgeeks.com/tutorial/rotate-programmer-matrix-border-python
반응형
'프로그래머스 > level 2' 카테고리의 다른 글
[프로그래머스] 다리를 지나는 트럭 (0) | 2022.06.01 |
---|---|
[프로그래머스] 기능개발 (0) | 2022.05.25 |
[프로그래머스] 거리두기 확인하기 (0) | 2022.05.12 |
[프로그래머스] 프린터 (0) | 2022.05.11 |
[프로그래머스] 튜플 (0) | 2022.04.28 |