๊ฐœ๋ฐœ/๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

programmers. ํ–‰๋ ฌ ํ…Œ๋‘๋ฆฌ ํšŒ์ „ํ•˜๊ธฐ

ttoance 2022. 6. 26. 18:42

https://programmers.co.kr/learn/courses/30/lessons/77485?language=java# 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ–‰๋ ฌ ํ…Œ๋‘๋ฆฌ ํšŒ์ „ํ•˜๊ธฐ

6 6 [[2,2,5,4],[3,3,6,6],[5,1,6,3]] [8, 10, 25] 3 3 [[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]] [1, 1, 5, 3]

programmers.co.kr

 

๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

 

๋‚ด ํ’€์ด  (2H)
import java.util.Arrays;
class Solution {
    int[] dx = {0, 1, 0, -1};
    int[] dy = {1, 0, -1, 0};
    int[][] origin;
    int[][] copy;
    
    public int[] solution(int rows, int columns, int[][] queries) {
        ROWS = rows;
        
        
        // ๋ฐฐ์—ด ์„ธํŒ… 
        origin = new int[rows][columns];
        for (int i = 0; i < rows; i++) {
          for (int j = 0; j < columns; j++) {
                origin[i][j] = (i * columns) + (j + 1);   // debug#4 : rows -> columns
          }
        }
       
        // debug#1 ๋ฐฐ์—ด ์ฐธ์กฐ๋กœ ๊ฑธ๋ฉด ์•ˆ๋จ
        copy = new int[rows][columns];
        for (int i = 0; i < rows; i++) {
          for (int j = 0; j < columns; j++) {
                copy[i][j] = origin[i][j];
          }
        }
        
        int[] answers = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
        	// debug#5 : ์˜ˆ์‹œ์™€ ๊ฐ’ ๋ณด์ • ์•ˆํ•ด์คŒ
            int answer  = rotate(queries[i][0] - 1, queries[i][1] - 1, queries[i][2] - 1, queries[i][3] - 1);
            
            // debug#2 : ๋ฐฐ์—ด ์ฐธ์กฐ๋กœ ๊ฑธ๋ฉด ์•ˆ๋จ 
            for (int di = 0; di < rows; di++) {
              for (int dj = 0; dj < columns; dj++) {
                origin[di][dj] = copy[di][dj];  
              }
            }   

           answers[i] = answer;
            
        }
        return answers;
    }
    
    public int rotate(int sx, int sy, int ex, int ey) {
        int temp = 0; 
        int currentX = sx; 
        int currentY = sy; // debug #3 : sx,sy๋Š” ๊ณ ์ •๊ฐ’์ž„ ๋ฐ”๋€Œ๋ฉด ์•ˆ๋จ. 
        
        int min = Integer.MAX_VALUE;
        // ์˜ค๋ฅธ์ชฝ ์•„๋ž˜์ชฝ ์™ผ์ชฝ ์œ„์ชฝ 
        while (temp < 4) {
            int nextx = currentX + dx[temp];
            int nexty = currentY + dy[temp];            
            
            // ์ข…๋ฃŒ์กฐ๊ฑด 
            if (dx[temp] != 0 && (nextx < sx || nextx > ex)) {
                temp ++;
                continue; 
            }
            else if (dy[temp] != 0 && (nexty < sy || nexty > ey)) {
                temp ++;
                continue; 
            }

            copy[nextx][nexty] = origin[currentX][currentY];
            min = Math.min(min, origin[currentX][currentY]);
            
            currentX = nextx;
            currentY = nexty;
        }
            
        return min;
    }
    
}

1. debug #1, #2 : ์ž๋ฐ”์—์„œ ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ a=b ์ด๋ ‡๊ฒŒ ํ•ด๋ฒ„๋ฆฌ๋ฉด ์–•์€ ๋ณต์‚ฌ๋ฅผ ํ•ด๋ฒ„๋ฆผ 

> Object.clone()์„ ํ†ตํ•ด์„œ 1์ฐจ ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ ๊นŠ์€ ๋ณต์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‚˜ 

> 2์ฐจ ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ, ์ด ํ•จ์ˆ˜๋„ ํ†ตํ•˜์ง€ ์•Š์•„์„œ ์ด์ค‘ for๋ฌธ์„ ๋Œ๋ฉด์„œ ํ•ด์•ผ ํ–ˆ์Œ. 

 

[Java] ์ž๋ฐ” ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ• (๊นŠ์€๋ณต์‚ฌ, ์–•์€๋ณต์‚ฌ)

์ž๋ฐ”์—์„œ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ์œ ํ˜•์œผ๋กœ ๊นŠ์€ ๋ณต์‚ฌ์™€ ์–•์€ ๋ณต์‚ฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊นŠ์€ ๋ณต์‚ฌ์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด์˜ ์‹ค์ œ๊ฐ’์„ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ด๊ณ  ์–•์€ ๋ณต์‚ฌ๋Š” ๋‹จ์ˆœํžˆ ๊ฐ์ฒด์˜ ์ฃผ์†Œ ๊ฐ’๋งŒ์„ ๋ณต์‚ฌํ•˜๋Š”

coding-factory.tistory.com

 

2. debug #3 : ๊ฒฝ๊ณ„๊ฐ’ ๋ฐ ๊ธฐ์ค€์ ์ธ sx, sy, ex, ey๋ฅผ for๋ฌธ์—์„œ ๋งค๋ฒˆ ์ดˆ๊ธฐํ™”ํ•ด์คŒ

> currentX, currentY๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š๋„๋ก ์ˆ˜์ • 

 

3. debug #4 : ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”ํ–ˆ์„๋•Œ ๋กœ์ง์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์—ˆ์Œ 

> origin[i][j] = (i * rows) + (j + 1);   ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด [3,4]์ผ ๋•Œ ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ง. rows๋ฅผ colums๋กœ ๊ณ ์ณ์•ผํ•จ. 

1 2 3 4 

4 5 6 7

7 8 9 10

10 11 12 13

 

4. debug#5 : ์˜ˆ์‹œ์—์„œ๋Š” ์ฒ˜์Œ์ด 1,1 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ–ˆ์ง€๋งŒ ๋‚˜๋Š” 0,0์—์„œ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์ •๊ฐ’์œผ๋กœ -1์„ ํ•ด์ฃผ์–ด์•ผ ํ•จ. 

 

 


 

 

๋‹ค์Œ์— ํ•ด๋ณผ๋งŒํ•œ ๊ฒƒ 

1. copy ๋ฐฐ์—ด ๋ง๊ณ  ๋‹ค๋ฅธ ๋ฐฉ๋ฒ• ์ฐพ์•„๋ณด๊ธฐ 

2. ํ ํ†ตํ•ด์„œ ํ’€์–ด๋ณด๊ธฐ 

๋ฐ˜์‘ํ˜•