Last active
July 12, 2018 02:48
-
-
Save NeuTrix/c23b03fbf75e7fcb9afa2c14d2adf5ad to your computer and use it in GitHub Desktop.
Cover "Manhattan skyline" using the minimum number of rectangles in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ===== ANSWER | |
| function solution(H) { | |
| // write your code in JavaScript (Node.js 8.9.4) | |
| let stack = []; | |
| let count = 0; | |
| let height = 0; | |
| // adds a block to the stack when value exceeds current height | |
| const addBlock = (value) => { | |
| if (value > height) { | |
| stack.push(value - height); | |
| height = value; | |
| count += 1; | |
| } | |
| } | |
| // adjust the blocks when value is lower than current height | |
| const popDown = (value) => { | |
| while (value < height) { // adjust to nearest block height | |
| height -= stack.pop(); | |
| } | |
| if (value > height) { | |
| addBlock(value) | |
| } | |
| } | |
| // loop through the set and make adjustments | |
| for (let i = 0; i < H.length; i += 1) { | |
| let value = H[i]; | |
| if (value < height) { | |
| popDown(value) | |
| } else if (value > height) { // ignore value equal to height | |
| addBlock(value) | |
| } | |
| } | |
| return count | |
| } | |
| // ===== PROBLEM | |
| https://app.codility.com/demo/results/trainingE6TPQZ-NMX/ | |
| - See detailed problem below | |
| // ===== Task description | |
| You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by an array H of N positive integers. H[I] is the height of the wall from I to I+1 meters to the right of its left end. In particular, H[0] is the height of the wall's left end and H[N−1] is the height of the wall's right end. | |
| The wall should be built of cuboid stone blocks (that is, all sides of such blocks are rectangular). Your task is to compute the minimum number of blocks needed to build the wall. | |
| Write a function: | |
| function solution(H); | |
| that, given an array H of N positive integers specifying the height of the wall, returns the minimum number of blocks needed to build it. | |
| For example, given array H containing N = 9 integers: | |
| H[0] = 8 H[1] = 8 H[2] = 5 | |
| H[3] = 7 H[4] = 9 H[5] = 8 | |
| H[6] = 7 H[7] = 4 H[8] = 8 | |
| the function should return 7. The figure shows one possible arrangement of seven blocks. | |
| ===== Assume that: | |
| N is an integer within the range [1..100,000]; | |
| each element of array H is an integer within the range [1..1,000,000,000]. | |
| Complexity: | |
| expected worst-case time complexity is O(N); | |
| expected worst-case space complexity is O(N) (not counting the storage required for input arguments). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment