본문 바로가기
알고리즘

[Programmers] 최소직사각형

by 슈슈슉민 2023. 6. 3.

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

 

프로그래머스

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

programmers.co.kr

첫번째 풀이

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Solution {
	public int solution(int[][] sizes) {
        List<Integer> minL = new ArrayList<>();
        List<Integer> maxL = new ArrayList<>();
        
        for (int i = 0; i < sizes.length; i++) {
        	// 세로
        	minL.add(Math.min(sizes[i][0], sizes[i][1]));
        	// 가로
        	maxL.add(Math.max(sizes[i][0], sizes[i][1]));
		}
        
        return Collections.max(minL)*Collections.max(maxL);
    }
}

두번째 풀이

public class Solution {
	public int solution(int[][] sizes) {
		int[] minL = new int[sizes.length];
		int[] maxL = new int[sizes.length];
        
        for (int i = 0; i < sizes.length; i++) {
        	// 세로
        	minL[i] = Math.min(sizes[i][0], sizes[i][1]);
        	// 가로
        	maxL[i] = Math.max(sizes[i][0], sizes[i][1]);
		}
        
        // 최대값
        int max1 = Integer.MIN_VALUE;
        int max2 = Integer.MIN_VALUE;
        
        for (int i = 0; i < minL.length; i++) {
			if(minL[i] > max1) {
				max1 = minL[i];
			}
			// 동시 진행
			if(maxL[i] > max2) {
				max2 = maxL[i];
			}
		}
        
        return max1 * max2;
    }
}

 

'알고리즘' 카테고리의 다른 글

[알고리즘 문제 해결 전략] 20장 문자열  (0) 2023.06.16
[BOJ]4358 생태학  (0) 2023.06.09
[알고리즘 문제해결 전략] 재귀함수  (1) 2023.06.09
[Programmers] 카펫  (0) 2023.06.07
[BOJ]14889 스타트와 링크  (0) 2023.06.03