코테 준비 21일차_9/8

Jieun·2026년 9월 8일

코딩테스트 준비

목록 보기
14/24
  1. 이름이 없는 동물의 아이디
  • 최종 정답
SELECT ANIMAL_ID FROM ANIMAL_INS WHERE NAME IS NULL
  1. 최소직사각형
  • 초기 정답
class Solution {
    public int solution(int[][] sizes) {
        for(int i=0;i<sizes.length;i++) {
            int w = sizes[i][0];
            int h = sizes[i][1];
            int w1 = 0;
            int h1 = 0;
            if(w > w1) {
                w1 = w;
            }
            if(h > h1) {
                h = h1;
            }
    return h * w;
        }
        
    }
}
  • 최종 정답
class Solution {
    public int solution(int[][] sizes) {
        int maxWidth = 0;
        int maxHeight = 0;
        for(int [] size : sizes) {
            int w = Math.max(size[0], size[1]);
            int h = Math.min(size[0], size[1]);
            
            maxWidth = Math.max(maxWidth, w);
            maxHeight = Math.max(maxHeight, h);
          

        }
        return maxWidth * maxHeight;
    }
}
  • 다른 정답
class Solution {
    public int solution(int[][] sizes) {
        int length = 0, height = 0;
        for (int[] card : sizes) {
            length = Math.max(length, Math.max(card[0], card[1]));
            height = Math.max(height, Math.min(card[0], card[1]));
        }
        int answer = length * height;
        return answer;
    }
}
  1. 공원 산책
  • 최종 정답
class Solution {
    public int[] solution(String[] park, String[] routes) {
        int H = park.length;
        int W = park[0].length();
        int r = 0, c=0;
        
        for(int i = 0; i<H; i++) {
            for(int j = 0; j<W;j++) {
                if(park[i].charAt(j) == 'S') {
                    r = i;
                    c = j;
                    break;
                }
            }
        }
        int[] dr = {-1, 1, 0, 0};
        int[] dc = {0, 0, -1, 1};
        
        for(String route : routes) {
            String[] split = route.split(" ");
            String dir = split[0];
            int count = Integer.parseInt(split[1]);
            
            int dirIdx = 0;
            if(dir.equals("N")) dirIdx = 0;
            else if (dir.equals("S")) dirIdx = 1;
            else if (dir.equals("W")) dirIdx = 2;
            else if (dir.equals("E")) dirIdx = 3;
            
            int nr = r;
            int nc = c;
            boolean canMove = true;
            
            for(int i=0;i<count;i++) {
                nr += dr[dirIdx];
                nc += dc[dirIdx];
                
                if(nr <0 || nr >= H || nc <0 || nc >= W || park[nr].charAt(nc) == 'X') {
                    canMove = false;
                    break;
                }
            }
            if(canMove) {
                r = nr;
                c = nc;
            }
            
        }
     return new int[] {r, c};
    }
}
profile
비전공자의 백엔드 생존기: 어제보다 1% 더 성장하기

0개의 댓글