How would I fix my approach to this Manhattan Skyline/Stone Wall and where did I go wrong? Javascript

I just came across this problem and thought I would give it a try, but now I'm stuck and need help if possible.

The problem I keep facing is my return is usually off by 1 or 2 but I can't figure out why not. I have traced my code back but still can't figure it out

The problem :

You are to write a program to assist an architect in drawing the skyline of a city. Building are rectangular in shape, the height of each building is represented by an element in a given array.

The above skyline above is represented like [1,3,2,1,2,1,5,3,3,4,2]

SO FAR HERE IS WHAT I AM WORKING WITH:

const skyline =(H)=> {
let stack = [];
let count = 0;
let height = 0;

 const addBlock = (value) => {
    if (value > height) {
        stack.push(value - height);
        height = value;
        count += 1;
    }
 }

 const pop = (value) => {
    while (value < height) {
        height -= stack.pop();
    }  
    if (value > height) {
        addBlock(value)
    }
 }

  for (let i = 0; i < H.length; i += 1) {
    let value = H[i];
    if (value < height) {
        pop(value)
    } else if (value > height) { 
        addBlock(value)
     }
 }

    return count
 }

 skyline([1,3,2,1,2,1,5,3,3,4,2]) //Expect 9

// Test CASES:

let strokes = [1,3,2,1,2,1,5,3,3,4,2] // Expect 9
// let strokes = [5,8] // Expect 8
// let strokes = [1,1,1,1] //  Expect 1

skyline(strokes)

Is this the basic algorithm?

* Big eats small (and equal-sized)

* Small reduces big to small
  adding the difference

* Count last one standing

Examples:

[5,8]
-> 8 eats 5, count 8

[1,1,1,1]
-> 1 eats 1 eats 1 eats 1
-> count 1

[1,3,2,1,2,1,5,3,3,4,2]
-> 3 eats 1
-> 2 reduces 3 to 2 and adds 3-2
-> 1 reduces 2 to 1 and adds 2-1
-> 2 eats 1
-> 1 reduces 2 to 1 and adds 2-1
-> 5 eats 1
-> 3 reduces 5 to 3 and adds 5-3
-> 3 eats 3
-> 4 eats 3
-> 2 reduces 4 to 2 and adds 4-2
-> count 2
Total: 1 + 1 + 1 + 2 + 2 + 2 = 9

JavaScript code:

function f(A){
  let result = 0;
  for (let i=1; i<A.length; i++)
    result += Math.max(0, A[i-1] - A[i]);
  return result + A[A.length-1];
}

console.log(f([1,3,2,1,2,1,5,3,3,4,2]));
console.log(f([5,8]));
console.log(f([1,1,1,1]));

[PDF] Stone wall, It's time to show you how the Codility challenge code-named Sigma can be solved. is a variant of a problem known as rectilinear skyline problem or Manhattan. I'm trying to understand the skyline problem. Given n rectangular building and we need to compute the skyline. I have trouble in understanding the output for this problem. Input: (1,11,5), (2,6,

function minimalNumberOfSkylinesIn(array) {

    if(array.length == 0) 
        return 0;

    let result = array[0];

    for(let i=1; i<array.length; ++i) {
        let differnce = array[i] - array[i-1];
        result += difference > 0 ? difference : 0;
    }

    return result;
}

Does the solar wind have escape velocity, This is just a selection--of the many questions that arrive, only a few are listed. Do Astrologers use Wrong Positions for Planets? How can the north wall of my house be in sunlight? The closest approach your friend mentions happened 2 years ago and was indeed NY skyline as a Stonehenge has some limits. How to Repair a Damaged Stone Wall. Rock walls serve as a timeless method of marking boundaries, retaining livestock and protection. Landscape designers and gardeners alike now use rock walls for similar reasons and find that using mortar or simply dry stacking them without a bonding agent adds tranquility to gardens.

the current answer seems to solve the problem presented, additionally I would like to point that a way to tackle this kind of problems is to solve it by hand and take notes on which steps you took to solve it.

In this case, they ask you to draw horizontal lines without picking up the pencil and one way to do that by hand is to do all the posible strokes on the same row before passing on to the next, until there are no rows left to check.

On every row, you will surely check if the current spot (array element) is greater than 0, which means that it is part of the stroke.

Now in more concise words:

  1. While there are rowsLeft I will traverse the array. on every traversal I will:
  2. check if the current position is greater than 0 which means there is a newStroke, it also means there are rowsLeft and since you want to keep moving forward you would like to decrease the current element by one.
  3. then, if there is a newStroke and the current element is 0 (end of the stroke) or if it is the end of the array, I would add 1 to my numOfStrokes count and also state that since I have just finished the stroke then there is no newStroke at the moment.

Well that's what I did to solve the case you posted, I believe you can code it from there and I hope it helps you, then again, bruce's answer seems to be right, I just wanted to add how you could came up with the solution, there are surely many ways f doing it.

[PDF] Port Phillip Heritage Review Update, o The information about the place in the relevant PPHR citation is correct; o The status of The methodology and approach to this review and its recommendations was also guided by: Street) is incorrect and should be 317-​329 Dorcas Street. HO147 Rail The walls are roughcast with brick details to the verandah piers. Fixing Fallen Stacked Stone. The basics of building stone walls can be traced back to when they were used as nothing more than boundaries or protective barriers for farms and homes. These days

Staff of The Wall Street Journal, The streets of downtown Manhattan were strewn with body parts, clothing, shoes and And the trade center towers had disappeared from the skyline. I feel like maybe it's a bad dream: If I wake up, I could get the Twin Towers back. That realization alone will alter the way the U.S. approaches its role in the world, as well  Wall murals and Wallpaper Murals of New York City, USA, Manhattan skyline, View from Staten Island F Photographer @ Alexander Demyanenko. Wall mural renvation of your grey Space

[PDF] Sprint: How to Solve Big Problems and Test New , The ideas that went on to launch and become successful were not story-​centered design to the sprint process, an unconventional approach that By Wednesday morning, sketches and notes plastered the walls of the Manhattan, and Brooklyn. We've included several stories in this book of sprints that went awry. The New York Skyline in Quotes Here is a list of our favorite quotes about the New York skyline -- so far. Remember that you can also use the search box at the bottom of this page, to find more quotes about the skyline (or any other aspect of NY).

Codility challenges java, Be the first to submit a golden solution and unlock not only a Codility award but see if different solutions can be created for challe Codility Solutions in JavaScript. Dec 31, 2014 · Short Problem Definition: Cover "Manhattan skyline" using the through one of the challenges from codility but the value returned is wrong. Put the New York skyline right on your wall. With our vast image selection of cities, times of day, and lighting, you’re sure to find a skyline image that fits the exact mood you desire. Take yourself to New York City, Paris, Tokyo, Chicago, Las Vegas, and more! Explore our selection of city skyline wall murals and mural wallpaper below.

Comments
  • Sounds like some school assignment to me.... Can you at least explain what/where your problem is? You have provided images of the relevant source code but failed to explain what isn't working and what you have tried.
  • @NewToJS it's not a school assignment. The problem I keep facing is my return is usually off by 1 or 2 but I can't figure out why not. I have traced my code back but still can't figure it out.
  • Please describe your algorithm in words. It might help resolve the difference between code and expected behaviour.
  • @bruce: Please don't use images. Otherwise someone would maybe downvote you, see idownvotedbecau.se/imageofcode.
  • from my understanding of the problem, I would say your correct
  • @bruce so I guess one solution could be to try and code that. Could you describe the algorithm you tried to code (if it's different)?
  • Someone posted a much better explanation here. (@bruce)