본문 바로가기
Coding Test/Programmers

[Programmers] (2021 Dev-Matching: 웹 백엔드 개발자(상반기)) Lv 2. 행렬 테두리 회전하기

by song.ift 2023. 3. 15.

https://school.programmers.co.kr/learn/courses/30/lessons/77485

 

프로그래머스

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

programmers.co.kr

 

function solution(rows, columns, queries) {
    const arr = [...Array(rows)].map((v, r) =>
        [...Array(columns)].map((v, c) => r * columns + (c + 1))
    ); // 먼저 2차원 배열에 값을 초기화한다.
    return queries.reduce((acc, querie) => {
        const [x1, y1, x2, y2] = querie;
        const move = [x2 - x1, y2 - y1]; // 이동할 x의 길이, y의 길이
        const [dirX, dirY] = [ // 시계방향 순
            [0, 1, 0, -1],
            [1, 0, -1, 0],
        ];

        let [x, y] = [x1 - 1, y1 - 1]; // 2차원 배열상에서의 x, y 인덱스
        let temp = arr[x][y], min = Infinity;
        const swap = (a) => {
            const j = a;
            a = temp;
            temp = j;
            return a;
        };

        for (let i = 0; i < 4; ++i) {
            let cnt = move[(i + 1) % 2]; // 시계방향 순으로 y먼저 이동할 것이기 때문에, i에 +1

            while (cnt--) { // 이동할 카운트가 0이 될때까지 반복
                (x += dirX[i]), (y += dirY[i]); // 현재 위치에서 시계방향순으로 새로운 위치를 갱신
                arr[x][y] = swap(arr[x][y]); // 이전 위치의 값과 현재의 값을 swap
                min = Math.min(min, arr[x][y]); // 가장 낮은 값으로 갱신
            }
        }
        acc.push(min);
        return acc;
    }, []);
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/tree/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/lv2/77485.%E2%80%85%ED%96%89%EB%A0%AC%E2%80%85%ED%85%8C%EB%91%90%EB%A6%AC%E2%80%85%ED%9A%8C%EC%A0%84%ED%95%98%EA%B8%B0

 

GitHub - developeSHG/Algorithm-Baekjoon_Programmers: 백준 and 프로그래머스 소스코드

백준 and 프로그래머스 소스코드. Contribute to developeSHG/Algorithm-Baekjoon_Programmers development by creating an account on GitHub.

github.com

 

댓글