title: manhattan skyline cover failing some test cases
body: <p>I am doing exercises on codility. I have spent two days on this problem with no improvement in my score. I get 100% on my correctness score, but fail some performance tests because it returns the wrong answer (not because of time or space complexity). My wrong results are always less than the expected answer. Can anyone think of a case where I need to add a stone that I am missing?</p>
  
  <p>This is the prompt:</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 a <strong>zero-indexed array <code>H</code> of <code>N</code> positive integers</strong>.</p>
    
    <p><code>H[I]</code> is the height of the wall from <code>I</code> to <code>I+1</code> meters to the right of its left end. In particular, <code>H[0]</code> is the height of the wall's left end and <code>H[N−1]</code> 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 that, given a zero-indexed array <code>H</code> of <code>N</code> positive integers specifying the height of the wall, returns the minimum number of blocks needed to build it.</p>
  
  <pre><code>class Solution { public int solution(int[] H); }
  </code></pre>
    
    <p>For example, given array <code>H</code> containing <code>N = 9</code> integers:</p>
  
  <pre><code>  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    
  </code></pre>
    
    <p>The function should return 7. The figure shows one possible arrangement of seven blocks.</p>
    
    <p>Assume that:</p>
    
    <ul>
    <li><code>N</code> is an integer within the range <code>[1..100,000]</code>;</li>
    <li>Each element of array <code>H</code> is an integer within the range <code>[1..1,000,000,000]</code>.</li>
    </ul>
    
    <p>Complexity:</p>
    
    <ul>
    <li>Expected worst-case time complexity is \$O(N)\$;</li>
    <li>Expected worst-case space complexity is \$O(N)\$, beyond input storage (not counting the storage required for input arguments).</li>
    <li>Elements of input arrays can be modified.</li>
    </ul>
  </blockquote>
  
  <p>This is my solution:</p>
  
  <pre><code> import java.util.*;
  
  class Solution {
      public int solution(int[] H) {
          // write your code in Java SE 8
  
          LinkedList&lt;Integer&gt; stack = new LinkedList&lt;Integer&gt;();
          int count = 1;
          int lowest = H[0];
          stack.push(H[0]);
  
          if(H.length == 1){
              return 1;   
          }
          else{
          for(int i = 1; i&lt;H.length; i++){
              if(H[i] &gt; H[i-1]){
                  stack.push(H[i]);
                  count++;   
              }
              if(H[i] &lt; lowest){
                  while(stack.size() &gt; 0){
                      stack.pop();   
                  }
                  stack.push(H[i]);
                  lowest = H[i];   
                  count++;
              }
              if(H[i] &lt; H[i-1] &amp;&amp; H[i] &gt; lowest){
                  while(stack.size() &gt; 0 &amp;&amp; stack.peek() &gt; H[i]){
                      stack.pop();   
                  }
                  if(stack.size() &gt; 0 &amp;&amp; stack.peek() &lt; H[i]){
                      stack.push(H[i]);
                      count++;   
                  }
              }
          }
          }
  
          return count;
      }
  }
  </code></pre>