i need to achic\ve this code same finction and get the smae result but i need to make the excution time liss a lot several times even you need to change the code import java.util.*;
import java.util.concurrent.TimeUnit;

class RectangleGridPermutations {
    private static Set<String> uniquePositioningStates = new HashSet<>();
    private static Rectangle[] rectangles;
    private static String[][] grid;
    private static int totalArea;

    public static void main(String[] args) {
        long startTime = System.nanoTime();
        rectangles = new Rectangle[]{
                new Rectangle(2, 1, "A"),
                new Rectangle(2, 1, "B"),
                new Rectangle(3, 1, "C"),
                new Rectangle(3, 5, "D"),
                new Rectangle(4, 3, "E"),
                new Rectangle(4, 2, "F"),
                new Rectangle(7, 1, "G"),
                new Rectangle(7, 1, "H"),
                new Rectangle(1, 8, "I"),
                new Rectangle(8, 2, "J"),
                new Rectangle(3, 2, "K"),
                new Rectangle(5, 2, "L"),
//                new Rectangle(8, 1, "M"),
//                new Rectangle(8, 1, "N"),
//                new Rectangle(8, 1, "O"),



        };

        Arrays.sort(rectangles, (r1, r2) -> Integer.compare(r2.space, r1.space));
        totalArea = calculateTotalArea(rectangles);

         List<Integer> heights = new ArrayList();
        List<Integer> widths = new ArrayList();
        for (Rectangle rectangle : rectangles) {
            widths.add(rectangle.width);
            heights.add(rectangle.height);
        }
        Collections.sort(heights);
        Collections.sort(widths);

        // Try all possible grid dimensions that can fit the total area
        for (int gridHeight = heights.get(heights.size()-1); gridHeight <= totalArea; gridHeight++) {
            if (totalArea % gridHeight == 0&(totalArea / gridHeight)>=widths.get(widths.size()-1)) {
                int gridWidth = totalArea / gridHeight;
                grid = new String[gridHeight][gridWidth];
                backtrack(0, gridHeight, gridWidth);
            }
        }

        if (uniquePositioningStates.isEmpty()) {
            System.out.println("No possible positioning states.");
        } else {
//            for (String state : uniquePositioningStates) {
//                printGrid(state);
//                System.out.println();
//            }
//            for (String state : uniquePositioningStates) {
//                printGridWithSpaces(state);
//                System.out.println();
//            }
            System.out.println("Possible positioning states number: " + uniquePositioningStates.size());
        }

        long endTime = System.nanoTime();
        System.out.println("Elapsed time: " + TimeUnit.NANOSECONDS.toMillis(endTime - startTime) / 1000 + "."
                + (TimeUnit.NANOSECONDS.toMillis(endTime - startTime) % 1000) + " seconds");
    }

    private static void backtrack(int idx, int gridHeight, int gridWidth) {
        if (idx == rectangles.length) {
            String gridState = toString(grid);
            uniquePositioningStates.add(gridState);
            return;
        }
        int rowMax = gridHeight;
        int colMax = gridWidth;

        if (idx != 0) {
            int lastUsedRow = 0, lastUsedCol = 0;

            for (int i = 0; i < gridHeight; i++)
                for (int j = 0; j < gridWidth; j++) {
                    if (grid[i][j] != null) {
                        lastUsedRow = Math.max(lastUsedRow, i + 1);
                        lastUsedCol = Math.max(lastUsedCol, j + 1);
                    }
                }
            rowMax = Math.min(rowMax, totalArea / lastUsedCol + 1);
            colMax = Math.min(colMax, totalArea / lastUsedRow + 1);
        }
        Rectangle rect = rectangles[idx];
        for (int row = 0; row <= rowMax - rect.height; row++) {
            for (int col = 0; col <= colMax - rect.width; col++) {
                if (canPlaceRectangle(row, col, rect, gridHeight, gridWidth)) {
                    placeRectangle(row, col, rect, true, gridHeight, gridWidth);
                    //if(checkSpaces(idx,gridHeight,gridWidth))
                        backtrack(idx + 1, rowMax, colMax);
                    placeRectangle(row, col, rect, false, gridHeight, gridWidth);
                }
            }
        }
    }

    private static boolean canPlaceRectangle(int row, int col, Rectangle rectangle, int gridHeight, int gridWidth) {
        for (int i = row; i < row + rectangle.height; i++) {
            for (int j = col; j < col + rectangle.width; j++) {
                if (grid[i][j] != null) {
                    return false;
                }
            }
        }
        return true;
    }
    public static boolean checkSpaces(int index,int gridHeight,int gridWidth) {
        int lastUsedRow = 0;
        int lastUsedCol = 0;
        for (int i = 0; i < gridHeight; i++)
            for (int j = 0; j < gridWidth; j++)
                if (grid[i][j] != null) {
                    lastUsedRow = Math.max(lastUsedRow, i + 1);
                    lastUsedCol = Math.max(lastUsedCol, j + 1);
                }
        int space = lastUsedRow * lastUsedCol;
//
//        int remindsSpace =calculateTotalArea2(Arrays.copyOfRange(rectangles, index, rectangles.length));
//        int placedSpace =calculateTotalArea2(Arrays.copyOfRange(rectangles, 0, index));
//        if((space-placedSpace)>remindsSpace)
//            return false;
//        if((space-placedSpace)>totalArea)
//            return false;

        return space <= totalArea;
    }

    private static void placeRectangle(int row, int col, Rectangle rectangle, boolean place, int gridHeight, int gridWidth) {
        for (int i = row; i < row + rectangle.height; i++) {
            for (int j = col; j < col + rectangle.width; j++) {
                grid[i][j] = place ? rectangle.name : null;
            }
        }
    }
    public static int calculateTotalArea2(Rectangle[]rectangles) {
        int maxArea = 0;
        for (Rectangle rectangle : rectangles) {
            maxArea += rectangle.width * rectangle.height;
        }
        return maxArea;
    }
    private static int calculateTotalArea(Rectangle[] rectangles) {
        int total = 0;
        for (Rectangle rect : rectangles) {
            total += rect.space;
        }
        return total;
    }

    private static String toString(String[][] grid) {
        StringBuilder sb = new StringBuilder();
        for (String[] row : grid) {
            for (String cell : row) {
                sb.append(cell != null ? cell : " ");
            }
            sb.append("\n");  // Add new line for better readability in the stored state
        }
        return sb.toString();
    }

    private static void printGrid(String gridState) {
        System.out.println(gridState);
    }
    private static void printGridWithSpaces(String gridState) {

        for(char c :gridState.toCharArray())
            System.out.print(" "+c);
    }
    static class Rectangle {
        int width;
        int height;
        String name;
        int space;

        public Rectangle(int width, int height, String name) {
            this.width = width;
            this.height = height;
            this.name = name;
            this.space = width * height;
        }
    }}