i need to achic\ve this code same finction and get the smae result but i need to make…
- User
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 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 heights = new ArrayList(); List 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); } } } }
- Assistant
- User
it is getting out of memory error fix it
- Assistant