SHARE
TWEET

Untitled

a guest Apr 26th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Skyline problem */
  2.  
  3. var input0 = [[1,4,3],[3,6,4]];
  4.             // [first x, second x, height]
  5. var input1 = [[1,4,3],[3,6,4],[5,8,5],[7,10,6]];
  6.  
  7. function findSkylines(input, knownPoints){
  8.     // base cases
  9.     if(knownPoints.length === 0){
  10.         let startPoint = [input[0], input[2]];
  11.         let endPoint = [input[1], 0];
  12.  
  13.         let array = [];
  14.         array.push(startPoint);
  15.         array.push(endPoint);
  16.  
  17.         return array;
  18.     }
  19.    
  20.     // other
  21.     let pointsArray = [];
  22.     let startToAdd = [input[0], input[2]];
  23.     let endToAdd = [input[1], 0];
  24.     for(let i = 0; i<knownPoints.length-1; i++){
  25.         var currentPoint = knownPoints[i];
  26.  
  27.         pointsArray.push(currentPoint);        
  28.  
  29.         if(startToAdd[0] > currentPoint[0] && startToAdd[0] < knownPoints[i+1][0]){
  30.             let newPoint = [startToAdd[0], input[2]];
  31.             pointsArray.push(newPoint);
  32.             console.log('Pushing: ');
  33.             console.log(newPoint);
  34.         }
  35.  
  36.         endToAdd = [Math.max(currentPoint[0], endToAdd[0]), 0];
  37.     }
  38.  
  39.     pointsArray.push(endToAdd);
  40.     return pointsArray;
  41. }
  42.  
  43.  
  44. function driver(input){
  45.  
  46.     let outputPoints = [];
  47.     let knownPoints = [];
  48.  
  49.     for(let i = 0; i < input.length; i++){
  50.         let buildingToAdd = input[i];
  51.         knownPoints = findSkylines(buildingToAdd, knownPoints);
  52.     }
  53.  
  54.     outputPoints = knownPoints;
  55.     return outputPoints;
  56. }
  57.  
  58. console.log(driver(input1));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top