You are on page 1of 2

public class BigCitySkyline { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub

int area = 0; ArrayList<Skyline> city = new ArrayList<Skyline>(); if (args.length > 0) { // We load the data according to the arguments int buildings = Integer.parseInt(args[0]); for (int i=1; i<args.length; i=i+2) { Skyline skyline = new Skyline(Integer.parseInt(args[i]) , Integer.parseInt(args[i+1])); city.add(skyline); } // At this point we have an Arraylist with all the skylines. // Now we will start looking for the biggest area possible. int currentHeight; int currentWidth; for (int i=0;i<city.size();i++) { // Get the current skyline and its properties Skyline current = city.get(i); currentHeight = current.getHeight(); currentWidth = current.getWidht(); // Now we go back and see if we can add some area from previous skylines. // This only will happen if the height of previous skyline is greater than or equal // to the current skyline. It will stop when find a skyline which height is less than the // current one or reach the first skyline. if (i > 0) { for (int j=i-1;j>-1;j--) { Skyline previous = city.get(j); if (previous.getHeight() < currentHeight) break; else currentWidth += previous.getWidht(); } } // Now we do exactly the same, but in the other direction. if (i < city.size()-1) { for (int j=i+1;j<city.size();j++) { Skyline next = city.get(j); if (next.getHeight() < currentHeight) break; else currentWidth += next.getWidht(); } } // Now if the current area is bigger than the one we hav e calculated so far, // then we set the new value to the area if (area < (currentHeight*currentWidth)) area = (currentHeight*currentWidth); }

System.out.println("The biggest area in this city is: " + area); } } }

You might also like