일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Programmers
- background: url
- context
- hook
- Nextjs
- notFound()
- codingtest
- react hook
- React 고급안내서
- react
- react-helmet
- Next13
- RTK Query
- background setttimeout
- Babel
- background setInterval
- Render Props
- getUTCDate
- React 18
- React18
- CSS
- React 공식문서
- 고급안내서
- React 18 Nextjs
- React 고급 안내서
- Nextjs React 18
- React API 참고서
- Javascript
- next13 head
- background tab
Archives
- Today
- Total
akjfal
[Programmers] 행렬 테두리 회전하기 본문
class Solution {
int[][] map;
int[] answer;
public int[] solution(int rows, int columns, int[][] queries) {
answer = new int[queries.length];
map = new int[rows][columns];
int num = 0;
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
map[i][j] = ++num;
}
}
for(int i = 0; i < queries.length; i++){
for(int j = 0; j < 4; j++){
queries[i][j]--;
}
rotate(queries[i], i);
}
return answer;
}
void rotate(int[] point, int index){
int nextNum = map[point[0]][point[1]];
int ans = nextNum;
// top line
for(int i = point[1]+1; i <= point[3]; i++){
int tmp = map[point[0]][i];
map[point[0]][i] = nextNum;
nextNum = tmp;
if(nextNum < ans){
ans = nextNum;
}
}
// right line
for(int i = point[0]+1; i <= point[2]; i++){
int tmp = map[i][point[3]];
map[i][point[3]] = nextNum;
nextNum = tmp;
if(nextNum < ans){
ans = nextNum;
}
}
// bottom line
for(int i = point[3]-1; i >= point[1]; i--){
int tmp = map[point[2]][i];
map[point[2]][i] = nextNum;
nextNum = tmp;
if(nextNum < ans){
ans = nextNum;
}
}
// right line
for(int i = point[2]-1; i >= point[0]; i--){
int tmp = map[i][point[1]];
map[i][point[1]] = nextNum;
nextNum = tmp;
if(nextNum < ans){
ans = nextNum;
}
}
answer[index] = ans;
}
}
단순한 구현문제다.
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 후보키 (0) | 2021.06.18 |
---|---|
[Programmers] 순위 검색 (0) | 2021.06.17 |
[Programmers] [1차] 뉴스 클러스터링 (0) | 2021.06.17 |
[Programmers] 튜플 (0) | 2021.06.17 |
[Programmers] 수식 최대화 (0) | 2021.06.17 |
Comments