coffee-yongsucheol / Java-Study-time

0 stars 0 forks source link

[하계 5주차] 코딩테스트 연습 #9

Open amazon7737 opened 3 months ago

junw00 commented 3 months ago

최소 직사각형

import java.util.*;
class Solution {
    public int solution(int[][] sizes) {
        List<Integer> l = new ArrayList<>();
        for(int[] i : sizes) {
            l.add(i[0]);
            l.add(i[1]);
        }
        Collections.sort(l);
        int m = l.get(l.size() - 1);
        for(int i = 0; i < l.size() - 1; i++) {
            for(int j = 0; j < sizes.length; j++) {
                int min = Math.min(sizes[j][0], sizes[j][1]);

                if(l.get(i) < min){
                    break;
                }

                if(j == sizes.length - 1) {
                    return m * l.get(i);
                }
            }
        }
        return 0;
    }
}
import java.util.*;
class Solution {
    public int solution(int[][] sizes) {
        int[] max = new int[sizes.length];
        int[] min = new int[sizes.length];

        for(int i = 0; i < sizes.length; i++) {
            max[i] = Math.max(sizes[i][0], sizes[i][1]);
            min[i] = Math.min(sizes[i][0], sizes[i][1]);
        }

        Arrays.sort(max);
        Arrays.sort(min);
        return max[max.length - 1] * min[min.length - 1];
    }
}
amazon7737 commented 3 months ago
import java.util.*;

class Solution {
    public int solution(int[][] sizes) {
        int answer = 0;

        for(int i=0; i< sizes.length; i++){
            if(sizes[i][0] < sizes[i][1]){
                int temp = sizes[i][1];
                sizes[i][1] = sizes[i][0];
                sizes[i][0] = temp;
            }
        }

        int[] left_array = new int[sizes.length];
        int[] right_array = new int[sizes.length];

        for(int i=0; i< sizes.length;i++){
            left_array[i] = sizes[i][0];
            right_array[i] = sizes[i][1];
        }

        int max_left = left_array[0];
        int max_right = right_array[0];

        for(int i=1; i< sizes.length;i++){
            if(max_left< left_array[i]){
                max_left = left_array[i];
            }

            if(max_right < right_array[i]){
                max_right = right_array[i];
            }
        }

        return max_left*max_right;
    }
}