akjfal

[Programmers] 행렬 테두리 회전하기 본문

알고리즘/programmers

[Programmers] 행렬 테두리 회전하기

akjfal 2021. 6. 17. 15:52
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