알고리즘

[Algorithm/Java] dev matching 행렬 테두리 회전하기

지수쓰 2021. 7. 14. 00:23
반응형
class Solution {
       public static int[] solution(int rows, int columns, int[][] queries) {
        int [][] board = new int[rows+2][columns+2];
        int cnt=1;
        for(int i=1; i<=rows; i++){
            for(int j=1; j<=columns; j++){
                board[i][j]=cnt++;
            }
        }


        int[] answer = new int[queries.length];
        for(int q=0; q<queries.length; q++){
            
            int a=queries[q][0];
            int b=queries[q][1];
            int c=queries[q][2];
            int d=queries[q][3];

            int max = 100001;
            int ab = board[a][b];
            int ad = board[a][d];
            int cb = board[c][b];
            int cd = board[c][d];

            for(int i=d-1; i>=b; i--){
                if(max>board[a][i]) max = board[a][i];
                board[a][i+1]=board[a][i];
            }
            for(int i=c-1; i>=a+1; i--){
                if(max>board[i][d]) max = board[i][d];
                board[i+1][d] = board[i][d];
            }
            if(max>ad) max=ad;
            board[a+1][d] = ad;
            
            for(int i=b+1; i<=d-1; i++){
                if(max>board[c][i]) max = board[c][i];
                board[c][i-1]= board[c][i];
            }
            if(max>cd) max=cd;
            board[c][d-1] = cd;
            
            for(int i=a+1; i<=c-1; i++){
                if(max>board[i][b]) max = board[i][b];
                board[i-1][b]=board[i][b];
            }
            if(max>cb) max=cb;
            board[c-1][b] = cb;
            
            
            if(max>ab)max=ab;
            answer[q]=max;
        }

        return answer;
    }


}

 

100*100 인데 최댓값을 100으로잡는 바보가있다..!! 

-_--

이런건 뭔가 정답이 있다기보단 천천히 확실하게 해야되늗네 대충 풀고 말려다보니까 하루가넘도록 못풀다 겨우풀었다..