프로그래머스 - 행렬 테두리 회전하기

스브코·2021년 11월 8일

문제 출처: 프로그래머스 - 행렬 테두리 회전하기

문제 풀이

import java.util.*;

class Solution {
    public int[] solution(int rows, int columns, int[][] queries) {
        ArrayList<Integer> answer = new ArrayList<>();
        int [][] board = new int[rows][columns];
        int number = 1;
        for(int r = 0; r < rows; r++) {
            for(int c = 0; c < columns; c++) {
                board[r][c] = number;
                number++;
            }
        }
        for(int i = 0; i < queries.length; i++) {
            int startX = queries[i][0] - 1;
            int startY = queries[i][1] - 1;
            int endX = queries[i][2] - 1;
            int endY = queries[i][3] - 1;
            
            int start = board[startX][startY];
            int min = start;
            int next = start;
            
            if(startY + 1 < columns) {
                for(int j = startY + 1; j <= endY; j++) {
                    int temp = board[startX][j];
                    board[startX][j] = next;
                    next = temp;
                    if(next < min)
                        min = next;
                }
            }

            if(startX + 1 < rows) {
                for(int j = startX + 1; j <= endX; j++) {
                    int temp = board[j][endY];
                    board[j][endY] = next;
                    next = temp;
                    if(next < min)
                        min = next;
                }
            }
            if(endY - 1 >= 0)   {
                for(int j = endY - 1; j >= startY; j--) {
                    int temp = board[endX][j];
                    board[endX][j] = next;
                    next = temp;
                    if(next < min)
                        min = next;
                }
            }
            if(endX - 1 >= 0) {
                for(int j = endX - 1; j >= startX; j--) {
                    int temp = board[j][startY];
                    board[j][startY] = next;
                    next = temp;
                    if(next < min)
                        min = next;
                }
            }
            answer.add(min);
        }
        int[] ans = new int [answer.size()];
        for(int n = 0; n < ans.length; n++)
            ans[n] = answer.get(n);
        
        return ans;
    }
}

풀이 해설

일단 보드판을 만들었다.

for(int r = 0; r < rows; r++) {
            for(int c = 0; c < columns; c++) {
                board[r][c] = number;
                number++;
            }
        }

회전 좌표 세트가 들어있는 2차원 배열을 돌건데,

 for(int i = 0; i < queries.length; i++) {

일단 좌표를 x, y 형태로 변수처리해 두었다.(헷갈리지 않기위해)

 int startX = queries[i][0] - 1;
            int startY = queries[i][1] - 1;
            int endX = queries[i][2] - 1;
            int endY = queries[i][3] - 1;

오른쪽 방향으로 가로, 아래 방향으로 세로, 왼쪽 방향으로 가로, 위 방향으로 세로 회전을 할건데,

index out of bound exception을 방지하기 위해

if(startY + 1 < columns) {

회전마다 if 문을 넣었고

회전마다 한 칸씩 값이 이동 되어야 하는데 nested loop으로 이동시켰다.

	    int start = board[startX][startY];
            int min = start;
            int next = start;

            if(startY + 1 < columns) {
                for(int j = startY + 1; j <= endY; j++) {
                    int temp = board[startX][j];
                    board[startX][j] = next;
                    next = temp;
                    if(next < min)
                        min = next;
                }
            }

한 회전 마다 최소 값을 찾아서 모은 후 array로 바꾸어 return 하였다.

answer.add(min);

int[] ans = new int [answer.size()];
        for(int n = 0; n < ans.length; n++)
            ans[n] = answer.get(n);
profile
익히는 속도가 까먹는 속도를 추월하는 그날까지...

0개의 댓글