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;
codepublic 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"),
code}; 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 { - Assistant