Muscardinus

행렬 테두리 회전하기 본문

알고리즘 문제/[프로그래머스] Lv2

행렬 테두리 회전하기

Muscardinus 2022. 1. 23. 23:26
728x90

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

 

코딩테스트 연습 - 행렬 테두리 회전하기

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

function solution(rows, columns, queries) {
    const answer = [];
    let arr = Array.from(new Array(rows + 1), () => new Array(columns + 1).fill(0));
    for (let y = 1; y <= rows; y++) {
        for (let x = 1; x <= columns; x++) {
            arr[y][x] = (y - 1) * columns + x;
        }
    }
    for (let i = 0; i < queries.length; i++) {
        const [x1, y1, x2, y2] = queries[i];
        const stack = [];
        for (let j = y1; j < y2; j++) stack.push(arr[x1][j]);
        for (let j = x1; j < x2; j++) stack.push(arr[j][y2]);
        for (let j = y2; j > y1; j--) stack.push(arr[x2][j]);
        for (let j = x2; j > x1; j--) stack.push(arr[j][y1]);
        answer.push(Math.min(...stack));
        const tmp = stack.pop();
        stack.unshift(tmp);
        for (let j = y1; j < y2; j++) arr[x1][j] = stack.shift();
        for (let j = x1; j < x2; j++) arr[j][y2] = stack.shift();
        for (let j = y2; j > y1; j--) arr[x2][j] = stack.shift();
        for (let j = x2; j > x1; j--) arr[j][y1] = stack.shift();
    }
    return answer;
}
728x90

'알고리즘 문제 > [프로그래머스] Lv2' 카테고리의 다른 글

순위검색  (0) 2022.02.02
거리두기 확인하기  (0) 2022.01.28
[3차] 방금그곡  (0) 2021.02.10
[3차] 압축  (0) 2021.02.07
이진 변환 반복하기  (0) 2021.02.06
Comments