Advertisement
Guest User

Solution to "I Failed a Twitter Interview"

a guest
Oct 30th, 2013
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3.  
  4. public class rainQ{
  5.     public static void main(String [] args){
  6.         int[][] heights = {
  7.                 {2,5,1,2,3,4,7,7,6},
  8.                 {3,2,1},
  9.                 {2,2,1,1,2},
  10.                 {5,3,2,1,2,3,4,5,4,3,0,5},
  11.                 {2,1,2,1,2},
  12.                 {3,2,1,2,1,2,0,2,3},
  13.                 {3,1,0,2},
  14.                 {},
  15.                 {0},
  16.                 {1,1},
  17.                 {0,0,0},
  18.                 {2,5,1,3,1,2,1,7,7,6},
  19.                 {5,3,2,1,2,3,4,5,4,3,0,4}
  20.                 };
  21.        
  22.        
  23.         int[] answers = {10,
  24.                 0,
  25.                 2,
  26.                 23,
  27.                 2,
  28.                 11,
  29.                 3,
  30.                 0,
  31.                 0,
  32.                 0,
  33.                 0,
  34.                 17};
  35.        
  36.         rainQ rQ = new rainQ();
  37.        
  38.         for(int i = 0; i < heights.length; i++){
  39.             boolean correct = (answers[i] == rQ.rainArea(heights[i]));
  40.             if(correct){
  41.                 System.out.println("Result " + i + " is " + correct);
  42.             } else {
  43.                 System.out.println("Result " + i + " is false.");
  44.             }
  45.             System.out.println();
  46.         }
  47.        
  48.     }
  49.    
  50.     public int rainArea(int[] heights){    
  51.         // There can't be a wall if there's only 2 elements
  52.         if(heights.length < 3){
  53.             return 0;
  54.         }
  55.        
  56.         // The derivative of the differences
  57.         int[] changeMatrix = new int[heights.length];
  58.        
  59.         // Set the first height as the first derivative
  60.         changeMatrix[0] = heights[0];
  61.  
  62.         boolean trap = false;
  63.        
  64.         int totalArea = 0, tempArea = 0;
  65.         int maxHeight = 0;
  66.         int length = 0;
  67.         boolean valid = false;
  68.        
  69.         // Calculate the rest of the heights
  70.         for(int i = 1; i < heights.length; i++){
  71.            
  72.             changeMatrix[i] = heights[i] - heights[i-1];
  73.            
  74.             if(trap == true) length++;
  75.            
  76.             if(trap==false && changeMatrix[i] < 0){
  77.                 maxHeight = heights[i-1];
  78.                 tempArea += (maxHeight - heights[i]);
  79.                 trap = true;
  80.                 //System.out.println(tempArea);
  81.             } else if(trap == true && maxHeight-heights[i] > 0 && !(i == heights.length-1)) {
  82.                 tempArea += (maxHeight - heights[i]);
  83.                 //System.out.println(tempArea);
  84.                 if(changeMatrix[i] > 0){
  85.                     valid = true;
  86.                 }
  87.             } else if(trap == true && maxHeight-heights[i] <= 0 || i == heights.length-1){
  88.                 if(changeMatrix[i]>0){
  89.                     valid = true;
  90.                 }
  91.                
  92.                 if(heights[i] < maxHeight){
  93.                     tempArea += length * (heights[i] - maxHeight);
  94.                     //System.out.println(tempArea);
  95.                 }
  96.                
  97.                 if(valid) totalArea+=tempArea;
  98.                 tempArea = 0;
  99.                 trap = false;
  100.                 maxHeight = heights[i];
  101.                 length = 1;
  102.                 valid = false;
  103.             }
  104.         }
  105.        
  106.         return totalArea;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement