title: Codility - Stonewall problem: 100% on correctness but 0% on performance
body: <p>I'm working on the stonewall exercise of codility. Getting 100% on the correctness tests, but failing all of the performance tests. I'm having trouble envisiging why my solution may be fine for smaller inputs but is going so wrong for larger inputs. Is anyone able to offer feedback about what might be wrong with my solution? I've found this one quite challenging. Taken me a few days of revisiting just to get to this stage! Thanks in advance.</p>
  <p><strong>The problem</strong></p>
  <blockquote>
  <p>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.</p>
  <p>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.</p>
  <p>Write a function:</p>
  <pre><code>function solution(H);
  </code></pre>
  <p>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.</p>
  <p>For example, given array H containing N = 9 integers:   H[0] = 8<br />
  H[1] = 8    H[2] = 5   H[3] = 7    H[4] = 9    H[5] = 8   H[6] = 7<br />
  H[7] = 4    H[8] = 8</p>
  <p>the function should return 7. The figure shows one possible
  arrangement of seven blocks.</p>
  <p>Write an efficient algorithm for the following assumptions:</p>
  <pre><code>    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].
  </code></pre>
  </blockquote>
  <p>My solution</p>
  <pre><code>function solution(H) {
  
      let stones = 0
      let absoluteMinimum = Infinity;
      let prevStones = []
      for (let i = 0; i &lt; H.length; i++) {
          if (H[i] &lt; absoluteMinimum) {
              stones ++
              absoluteMinimum = H[i]
              prevStones = [H[i]]
          } else if (prevStones.includes(H[i])) {
              while (prevStones.includes(H[i])) {
                  prevStones.pop()
              }
              prevStones.push(H[i])
          } else if (H[i] != H[i-1]) {
              prevStones.push(H[i])
              stones ++
          }
      }
      return stones
  }
  </code></pre>
  <p>Here is the summary of my attempt including test results.
  <a href="https://app.codility.com/demo/results/training2V8Y42-AUQ/" rel="nofollow noreferrer">https://app.codility.com/demo/results/training2V8Y42-AUQ/</a></p>