• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
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, input];
11.         let endPoint = [input, 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, input];
23.     let endToAdd = [input, 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 > currentPoint && startToAdd < knownPoints[i+1]){
30.             let newPoint = [startToAdd, input];
31.             pointsArray.push(newPoint);
32.             console.log('Pushing: ');
33.             console.log(newPoint);
34.         }
35.
36.         endToAdd = [Math.max(currentPoint, endToAdd), 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.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top