기본 콘텐츠로 건너뛰기

코딩 테스트 - n^2 배열 자르기

package programmers;

import java.util.Arrays;

public class 배열자르기_230108 {
    public static void main(String[] args) {
        int n = 3;
        long left = 2;
        long right = 5;
        solution(n, left, right);
    }

   
    public static int[] solution(int n, long left, long right) {
       
       
        int [][] arr = new int[n][n]; // n x n 배열을 만듭니다.
        int [] answer = new int[n * n];
        /*
         * 1 2 3        00 01 02
         * 2 2 3        10 11 12
         * 3 3 3        20 21 22  
         *
         * 0만 들어있음 = 1
         * 1도 들어있음 = 2
         * 2도 들어있음 = 3
         */

        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(i == j){
                    arr[i][j] = i + 1;
                }else{
                    arr[i][j] = i < j ? j+1 : i+1;
                    //j가 크면 j+1 / i가 크면 i+1
                }
                answer[n * i + j] = arr[i][j];
            }
        }

        int [] answerArr = Arrays.copyOfRange(answer,(int) left, (int) right+1);
       

        return answerArr;
    }

}


이렇게 짜면 메모리 초과가 나므로 고쳐본다!
문제가 시키는 대로 정직하게 짜면 100% 메모리 초과가 난다!

전체를 탐색하는 것이 아니라, 아예 쪼그만 배열을 만들고,
계산을 해서 Math.max 쓰는 것이 포인트...
(라고 행복하게 짰는데 한 줄짜리 코드를 보고 놀라워함!)

LongStream.rangeClosed(0, n*n-1).mapToInt(i -> (int) (Math.max(i / n, i % n) + 1)).toArray();

오... stream의 세계란 즐거운 것

아무튼 내가 만든 코드는 아래와 같음! ↓

package programmers;

public class 배열자르기_230108 {
    public static void main(String[] args) {
        int n = 3;
        long left = 2;
        long right = 5;
        solution(n, left, right);
    }

   
    public static int[] solution(int n, long left, long right) {
       
        int [] answer = new int[(int) (right-left+1)];

        /*
         * 00 01 02     1 2 3      
         * 10 11 12     2 2 3       1 2 3 2 2 3 3 3 3       02 10 11 12 -> 3  2 2  3
         * 20 21 22     3 3 3      
         */
 
       
        int [] ans = new int[2];
        for(int i = 0; i < answer.length; i++){
            ans[0] = (int)((i + left) / n) + 1; //
            ans[1] = (int)((i + left) % n) + 1; //
            //overflow가 나니까 필요한 값만 오려서 계산해야 한다.
            answer[i] = Math.max(ans[0], ans[1]);
            //System.out.println(answer[i] + "ㅇㅅㅇ" + ans[0] + "ㅇㅅㅇ" + ans[1]);
            /*
                2ㅇㅅㅇ0ㅇㅅㅇ2
                1ㅇㅅㅇ1ㅇㅅㅇ0
                1ㅇㅅㅇ1ㅇㅅㅇ1
                2ㅇㅅㅇ1ㅇㅅㅇ2
                에다가 +1 더하면 3 2 2 3 도출 가능-> Math.max 써서 해결함
                left 쓰는 것이 포인트이다.
             *
             */
        }
       

        return answer;

        /*
        int [] test = LongStream.rangeClosed(0, n*n-1).mapToInt(i -> (int) (Math.max(i / n, i % n) + 1)).toArray();
        for(int i : test){
            System.out.print(i + " ");
            //1 2 3 2 2 3 3 3 3
            //전체를 다 만들어줌
        }
        System.out.println();
        int [] test1 = LongStream.rangeClosed(left, right).mapToInt(i -> (int) (Math.max(i / n, i % n) + 1)).toArray();

        for(int i : test1){
            System.out.print(i + " ");
            //3 2 2 3
        }

        return LongStream.rangeClosed(left, right).mapToInt(value -> (int) (Math.max(value / n, value % n) + 1)).toArray();
        //답안 중에 대단한 것이 있어서 공부하려고 가져옴...
        //intstream 대신 longstream을 써서 rangeclosed 함수를 사용함 -> 인자값 생성해 주는 것
        //그러고 나서는 mapToInt 써서 Math.max(value / n, value % n) + 1)
        //stream 써서 한줄로 짠 것 respect...★
        */
    }

}



댓글