Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
899 B
4
Indexable
Never
public static int countGroups(int n, int[] length, int[] breadth) {
    int[][] rectangles = new int[length.length][2];
    for (int i = 0; i < length.length; i++) {
        rectangles[i][0] = length[i];
        rectangles[i][1] = breadth[i];
    }
    Arrays.sort(rectangles, new Comparator<int[]>() {
        @Override
        public int compare(int[] a, int[] b) {
            return (b[0]*b[1]) - (a[0]*a[1]);
        }
    });
    List<int[]> groups = new ArrayList<>();
    for (int[] r : rectangles) {
        boolean added = false;
        for (int[] g : groups) {
            if (r[0] < g[0] && r[1] < g[1]) {
                g[0] = Math.min(g[0], r[0]);
                g[1] = Math.min(g[1], r[1]);
                added = true;
                break;
            }
        }
        if (!added) {
            groups.add(new int[]{r[0], r[1]});
        }
    }
    return groups.size();
}