eaz_coding

[Programmers] 행렬 테두리 회전하기(Python, Javascript, Java) 본문

eaz_algorithm

[Programmers] 행렬 테두리 회전하기(Python, Javascript, Java)

eaz_silver 2024. 6. 7. 10:37

문제

요약

행렬 테두리를 시계방향으로 한칸씩 이동한다. 이동한 테두리에서 가장 작은 값을 순서대로 구하여라.

 

문제

https://school.programmers.co.kr/learn/courses/30/lessons/77485?language=python3

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


풀이

Python

from collections import deque
def solution(rows, columns, queries):
    answer = []
    arr = [[0] * columns for _ in range(rows)]
    
    for i in range(rows):
        for j in range(columns):
            arr[i][j] = i*columns + j+1;
    
    tmp = deque([])
    for query in queries:
        x1,y1,x2,y2 = query;
        
        for i in range(y1-1, y2):
            tmp.append(arr[x1-1][i])
        for i in range(x1, x2):
            tmp.append(arr[i][y2-1])
        for i in range(y2-2, y1-1, -1):
            tmp.append(arr[x2-1][i])      
        for i in range(x2-1, x1-1, -1):
            tmp.append(arr[i][y1-1])
        
        answer.append(min(tmp))
        
        for i in range(y1, y2):
            arr[x1-1][i] = tmp.popleft()
        for i in range(x1, x2):
            arr[i][y2-1] = tmp.popleft()
        for i in range(y2-2, y1-1, -1):
            arr[x2-1][i] = tmp.popleft()
        for i in range(x2-1, x1-2, -1):
            arr[i][y1-1] = tmp.popleft()
                
    return answer

 

Javascript

function solution(rows, columns, queries) {
    var answer = [];

    let arr = [];    
    for (let i = 0; i < rows; i++){
        let t = [];
        for (let j = 0; j < columns; j++){
            t.push(i*columns + j + 1);
        }
        arr.push(t);
    }
    
    let tmp = [];
    for (let query of queries){
        const [x1, y1, x2, y2] = query;
        for (let i = y1-1; i < y2; i++){
            tmp.push(arr[x1-1][i]);
        }
        for (let i = x1; i < x2; i++){
            tmp.push(arr[i][y2-1]);
        }
        for (let i = y2-2; i > y1-2; i--){
            tmp.push(arr[x2-1][i]);
        }
        for (let i = x2-2; i > x1-1; i--){
            tmp.push(arr[i][y1-1]);
        }
        
        answer.push(Math.min(...tmp));
        
        for (let i=y1; i < y2; i++){
            arr[x1-1][i] = tmp.shift();
        }
        for (let i = x1; i < x2; i++){
            arr[i][y2-1] = tmp.shift();
        }
        for (let i = y2-2; i > y1-2; i--){
            arr[x2-1][i] = tmp.shift();
        }
        for (let i = x2-2; i > x1-2; i--){
            arr[i][y1-1] = tmp.shift();
        }
    }
    
    return answer;
}

 

Java

import java.util.*;

class Solution {
    public int[] solution(int rows, int columns, int[][] queries) {
        int[] answer = new int[queries.length];
        int[][] arr = new int[rows][columns];
        for (int i=0; i<rows; i++){
            for (int j = 0; j<columns; j++){
                arr[i][j] = i*columns + j + 1;
            }
        }
        
        ArrayList<Integer> tmp = new ArrayList<>();
        int k = 0;
        for (int i = 0; i < queries.length; i++) {
            int x1 = queries[i][0];
            int y1 = queries[i][1];
            int x2 = queries[i][2];
            int y2 = queries[i][3];
            
            for (int j = y1-1; j < y2; j++){
                tmp.add(arr[x1-1][j]);
            }
            for (int j = x1; j < x2; j++){
                tmp.add(arr[j][y2-1]);
            }
            for (int j = y2-2; j > y1-2; j--){
                tmp.add(arr[x2-1][j]);
            }
            for (int j = x2-2; j > x1-1; j--){
                tmp.add(arr[j][y1-1]);
            }
            
            answer[k] = tmp.get(0);
            for (int n : tmp) {
               if (n < answer[k]) answer[k] = n;
            }    
            k++;
            
            for (int j = y1; j < y2; j++){
                arr[x1-1][j] = tmp.remove(0);
            }
            for (int j = x1; j < x2; j++) {
                arr[j][y2-1] = tmp.remove(0);
            }
            for (int j = y2-2; j > y1-2; j--){
                arr[x2-1][j] = tmp.remove(0);
            }
            for (int j = x2-2; j > x1-2; j--){
                arr[j][y1-1] = tmp.remove(0);
            }
        }
        return answer;
    }
}