[Java] 최소직사각형 - Lv1 프로그래머스 완전탐색 / 코딩테스트 고득점 Kit

P.S./프로그래머스 2023. 4. 16. 06:57
728x90
728x90

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

 

프로그래머스

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

programmers.co.kr

 

 

 

풀이


문제 자체는 어렵지 않았다.

1. sizes 배열에서 긴 쪽을 가로, 세로 중 한 곳으로 몰아넣음

2. 가로가 가장 큰 녀석, 세로가 가장 큰 녀석 곱해주면 끝.

 

첫 풀이

class Solution {
    public int solution(int[][] sizes) {        
        int[][] size = new int[sizes.length][2];
        for(int i=0; i<sizes.length; i++) {
        	size[i][0] = Math.max(sizes[i][0], sizes[i][1]);
        	size[i][1] = Math.min(sizes[i][0], sizes[i][1]);
        }
        int x = 0;
        int y =0;
        for(int[] i : size) {
        	x = Math.max(x, i[0]);
        	y = Math.max(y, i[1]);
        }
        return x*y;

    }
}

 

 

굳이 size 배열을 생성하지 않아도 처리할 수 있으므로 간결하게 수정했다.

 

 

 

class Solution {	
    
    public int solution(int[][] sizes) {
    	int x = 0;
    	int y = 0;
    	for(int[] size : sizes) {
        	x = Math.max(x, Math.max(size[0], size[1]));
        	y = Math.max(y, Math.min(size[0], size[1]));        
    	}    	
        return x*y;
    }
}​

 

728x90
300x250
mag1c

mag1c

2년차 주니어 개발자.

방명록