View

๋ฐ˜์‘ํ˜•

์‚ผ๊ฐ ๋‹ฌํŒฝ์ด

์ •์ˆ˜ n์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ฐ‘๋ณ€์˜ ๊ธธ์ด์™€ ๋†’์ด๊ฐ€ n์ธ ์‚ผ๊ฐํ˜•์—์„œ ๋งจ ์œ„ ๊ผญ์ง“์ ๋ถ€ํ„ฐ ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ๋‹ฌํŒฝ์ด ์ฑ„์šฐ๊ธฐ๋ฅผ ์ง„ํ–‰ํ•œ ํ›„, ์ฒซ ํ–‰๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ํ–‰๊นŒ์ง€ ๋ชจ๋‘ ์ˆœ์„œ๋Œ€๋กœ ํ•ฉ์นœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

!https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/e1e53b93-dcdf-446f-b47f-e8ec1292a5e0/examples.png


์ œํ•œ์‚ฌํ•ญ

  • n์€ 1 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

n result

4 [1,2,9,3,10,8,4,5,6,7]
5 [1,2,12,3,13,11,4,14,15,10,5,6,7,8,9]
6 [1,2,15,3,16,14,4,17,21,13,5,18,19,20,12,6,7,8,9,10,11]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #3

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๐Ÿ“ ๊ธฐ์กด ํ’€์ด


import java.util.*;

class Solution {
    public int[] solution(int n) {
        int [] dy={1,0,-1};
        int [] dx={0,1,-1};
        
        int num = 1;
        int[][] arr = new int [n][n];
        int x=0, y=0;
        
        while(true){
         // 1. ์•„๋ž˜๋กœ ์ด๋™ 
            while(true){
                arr[y][x]= num++;
                    
                int ny = y+dy[0];
                int nx = x+dx[0];
                
                if (ny==n || arr[ny][x] !=0) break;
                x=nx;
                y=ny;
            }
            
            if (x+1==n || arr[y][x+1] !=0) break;
            //์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ์นธ ์ด๋™
            x+=1;
            
        // 2. ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ 
            while(true){
                arr[y][x]= num++;
                    
                int ny = y+dy[1];
                int nx = x+dx[1];
                
                if (nx==n || arr[y][nx] !=0) break;
                x=nx;
                y=ny;   
            }
            if (y-1==-1 || x-1== -1 || arr[y-1][x-1] !=0) break;
            y-=1;
            x-=1;
            
        // 3. ๋Œ€๊ฐ์„  ์œ„๋กœ ์ด๋™
            while(true){
                arr[y][x]= num++;
                
                int ny = y+dy[2];
                int nx = x+dx[2];
                
                if (arr[ny][nx] != 0 || ny==-1 || nx == -1) break;
                x=nx;
                y=ny;   
                
            }
            if (arr[y+1][x] != 0 || y+1==n ) break;
            y+=1;
            
        }
        
        int[] answer = new int [num-1];
        int count=0;
        
        for (int i=0; i< n; i++){
            for (int j=0; j<n; j++){
                if (arr[i][j]!=0){
                answer [count++] = arr[i][j];
                }
            }
        }
        
        return answer;
    }
}

๐Ÿ‘๋” ๋‚˜์€ ํ’€์ด๐Ÿ‘

<aside> ๐Ÿ“Œ ์„ธ ๋ฐฉํ–ฅ์„ ๋”ฐ๋กœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ , ๋ฐฉํ–ฅ ๋ฒกํ„ฐ๋ฅผ ์ด์šฉํ•ด์„œ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•จ 0(์•„๋ž˜) → 1(์˜ค๋ฅธ์ชฝ ) → 2(์™ผ์ชฝ ์œ„)

// 2๋ฅผ ๋„˜์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด ๋‹ค์‹œ 0์œผ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ d=(d+1)%3

import java.util.*;

class Solution {
    private static final int []dx={0,1,-1};
    private static final int []dy={1,0,-1};
    
    
    public int[] solution(int n) {
        int d=0; // ๋ฐฉํ–ฅ์ฒ˜๋ฆฌ ๋ณ€์ˆ˜
        int num = 1;
        int[][] arr = new int [n][n];
        int x=0, y=0;
        
        while(true){
            arr[y][x]=num++;
            
            int nx = x+dx[d];
            int ny = y+dy[d];
            
            if (nx == n || ny == n|| nx == -1||ny == -1 || arr[ny][nx]!=0) {
                d =(d+1) % 3;
                nx = x+dx[d];
                ny = y+dy[d];
                
                if (nx == n || ny == n|| nx == -1|| ny == -1|| arr[ny][nx]!=0) break;
            }
            
            x=nx;
            y=ny;
        }
        
        int [] answer = new int [num - 1];
        int index =0;
        
        for (int i=0; i<n; i++){
            for (int j=0; j<=i; j++){
                answer[index++] = arr[i][j];
            }
        }
        
        
        return answer;
    }
}

</aside>

๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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