Notice
                              
                          
                        
                          
                          
                            Recent Posts
                            
                        
                          
                          
                            Recent Comments
                            
                        
                          
                          
                            Link
                            
                        
                    | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 31 | 
                            Tags
                            
                        
                          
                          - 스마트팩토리
- 현대
- 자바
- 알고리즘
- GAN
- programmers
- JavaScript
- 프로그래머스
- cs공부
- 오블완
- 파이썬
- 소프티어
- cim
- boj
- Python
- 토이프로젝트
- 탐욕법
- 자바스크립트
- re_lunchu
- alogorithm
- 백준
- softeer
- 티스토리챌린지
- Baekjoon
- 딥러닝
- Algorithm
- 힙큐
- heapq
- 현대자동차
- Java
                            Archives
                            
                        
                          
                          - Today
- Total
eaz_coding
[Programmers] 행렬 테두리 회전하기(Python, Javascript, Java) 본문
문제
요약
행렬 테두리를 시계방향으로 한칸씩 이동한다. 이동한 테두리에서 가장 작은 값을 순서대로 구하여라.
문제
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;
    }
}'eaz_algorithm' 카테고리의 다른 글
| [Programmers] 테이블 해시 함수(Python, Javascript) (0) | 2024.06.17 | 
|---|---|
| [Programmers] 미로 탈출(Python, Javascript, Java) (0) | 2024.06.10 | 
| [Programmers] 수식 최대화(Javascript) (0) | 2024.06.06 | 
| [Programmers] 괄호 변환(Javascript) (0) | 2024.06.05 | 
| [Programmers] 줄 서는 방법(JavaScript) (0) | 2024.06.05 | 
