[JAVA] 택배 상자 꺼내기

NoHae·2025년 5월 14일
0

문제 출처

https://school.programmers.co.kr/learn/courses/30/lessons/389478
프로그래머스 코딩테스트 > 연습문제

문제 설명

접근 방법

상자의 배치 방식을 2차원 배열로 두고, num에 해당하는 row, col을 찾은 후, 그 위의 상자가 놓여있는 갯수를 센다.

class Solution {
    public int solution(int n, int w, int num) {
        int answer = 0;
        int floor = n / w;
        if (n % w > 0) floor++; // 나머지 상자가 있으면 한 층 더 필요

        int box = 1;
        int[][] garage = new int[floor][w];

        // 상자 배치
        for (int i = 0; i < floor; i++) {
            if (i % 2 == 0) {
                for (int j = 0; j < w && box <= n; j++) {
                    garage[i][j] = box++;
                }
            } else {
                for (int j = w - 1; j >= 0 && box <= n; j--) {
                    garage[i][j] = box++;
                }
            }
        }

        // num 위치 찾기
        int row = -1, col = -1;
        for (int i = 0; i < floor; i++) {
            for (int j = 0; j < w; j++) {
                if (garage[i][j] == num) {
                    row = i;
                    col = j;
                    break;
                }
            }
            if (row != -1) break;
        }

        // 해당 열 위쪽 상자 개수 세기
        for (int i = row; i < floor; i++) {
            if (garage[i][col] != 0) {
                answer++;
            }
        }

        return answer;
    }
}

Review

class Solution {
    public int solution(int n, int w, int num) {
        int answer = 0;
        
        int floor = n/w;
        if(n%w > 0 ) floor++;
        
        int[][] boxes = new int[floor][w];
        
        int current = 1;
        
        int numFloor = 0;
        int numIndex = 0;
        
        for(int i = 0; i<floor; i++){
            if(i%2 == 0){
                for(int j = 0; j<w && current<=n; j++){
                    if(num == current){
                        numFloor = i;
                        numIndex = j;
                    }
                boxes[i][j] = current++; 
            }
                continue;
            }
            for(int j = w-1; j>=0 && current<=n; j--){
                if(num == current){
                    numFloor = i;
                    numIndex = j;
                }
                boxes[i][j] = current++;
            }
            
        }
        
        for(int i = numFloor; i<floor; i++){
            if(boxes[i][numIndex] > 0) answer++;
        }
        
        
        return answer;
    }
}

알게된 점

수학적인 규칙을 찾아 풀려고 했으나 실패했다.

class Solution {
    public int solution(int n, int w, int num) {
        int answer = 0;
        int additional = 0;
        if(n%w >0) additional++;
        int floor = n/w + additional;
        
        int numAdditional = 0;
        if(num%w > 0) numAdditional++;
        int numFloor = num/w + numAdditional;
        
        System.out.println(floor + " " + numFloor);
        
        if(n%w == num%w){
            if(num%w>=n%w) answer =floor-numFloor + 1;
            else answer = floor-numFloor;
        } else {
            if(num+(floor-numFloor)*w>n) answer = floor-numFloor;
            else answer = floor-numFloor;
        }
        return answer;
    }
} 

Review
문제를 풀 때, 놓치기 쉬운 부분은 2차원 배열을 생성할 때, 최대 n개의 박스까지만 배열에 집어넣어야하는 점인 것 같다.
해당 부분에서 한 번 틀렸다.

문제푼 흔적



Review

profile
노력 해보려고 하는 사람(00년생 소프트웨어융합학과, 24년 12월 부터 백엔드 및 코테 공부 시작)

0개의 댓글