Advertisement
dimipan80

Exam 3. Largest 3 Rectangles

Sep 10th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class _3_Largest3Rectangles {
  4.  
  5.     public static void main(String[] args) {
  6.         // TODO Auto-generated method stub
  7.         Scanner scan = new Scanner(System.in);
  8.         String inputSequence = scan.nextLine();
  9.  
  10.         inputSequence = inputSequence.replace("[", "");
  11.         inputSequence = inputSequence.replace(" ", "");
  12.         String[] rectangleSeq = inputSequence.split("]");
  13.  
  14.         int largestTotalArea = findTheLargestThreeConsecutiveRectangles(rectangleSeq);
  15.  
  16.         System.out.println(largestTotalArea);
  17.     }
  18.  
  19.     private static int findTheLargestThreeConsecutiveRectangles(
  20.             String[] rectangles) {
  21.         int maxArea = 0;
  22.         int totalArea;
  23.         for (int i = 0; i + 2 < rectangles.length; i++) {
  24.             totalArea = 0;
  25.             for (int j = i; j < i + 3; j++) {
  26.                 String[] sides = rectangles[j].split("x");
  27.                 totalArea += (Integer.parseInt(sides[0]) * Integer
  28.                         .parseInt(sides[1]));
  29.             }
  30.  
  31.             if (totalArea > maxArea) {
  32.                 maxArea = totalArea;
  33.             }
  34.         }
  35.  
  36.         return maxArea;
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement