Advertisement
svetlozar_kirkov

Largest Three Rectangles

Feb 6th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Scanner;
  8.  
  9. public class LargestThreeRectangles {
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner input = new Scanner(System.in);
  13.         String[] inputLine = input.nextLine().split("[\\[\\D\\W\\]\\s]+");
  14.         String[] inputLineCleaned = clean(inputLine);
  15.         ArrayList<String[]> rectSides = new ArrayList<>();
  16.         for (int i = 0; i < inputLineCleaned.length; i+=2){
  17.             String[] temp = {inputLineCleaned[i],inputLineCleaned[i+1]};
  18.             rectSides.add(temp);
  19.         }
  20.         Map<String,Long> combinations = new LinkedHashMap<>();
  21.         for (int i = 0; i < rectSides.size()-2; i++){
  22.             String[] tempOne = rectSides.get(i);
  23.             String[] tempTwo = rectSides.get(i+1);
  24.             String[] tempThree = rectSides.get(i+2);
  25.             long areaOne = Long.parseLong(tempOne[0])*Long.parseLong(tempOne[1]);
  26.             long areaTwo = Long.parseLong(tempTwo[0])*Long.parseLong(tempTwo[1]);
  27.             long areaThree = Long.parseLong(tempThree[0])*Long.parseLong(tempThree[1]);
  28.             long areaSum = areaOne+areaTwo+areaThree;
  29.             String key = tempOne[0]+"*"+tempOne[1]+" + "
  30.                     +tempTwo[0]+"*"+tempTwo[1]+" + "
  31.                     +tempThree[0]+"*"+tempThree[1]+" = ";
  32.             combinations.put(key, areaSum);
  33.         }
  34.         String test = Collections.max(combinations.entrySet(),
  35.                 (entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).getKey();
  36.         System.out.println(combinations.get(test));
  37.     }
  38.    
  39.     public static String[] clean(final String[] v) {
  40.         List<String> list = new ArrayList<String>(Arrays.asList(v));
  41.         list.removeAll(Collections.singleton(""));
  42.         return list.toArray(new String[list.size()]);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement