Covering a Skyline with brush strokes Given a non-negative integer skyline height list, answer how many uninterrupted 1-unit-high horizontal brush strokes are needed to cover it.

[1,3,2,1,2,1,5,3,3,4,2], visualised as:

      5    
      5  4 
 3    5334 
 32 2 53342
13212153342


needs nine brush strokes:

      1    
      2  3 
 4    5555 
 66 7 88888
99999999999


Examples

[1,3,2,1,2,1,5,3,3,4,2] → 9

[5,8] → 8

[1,1,1,1] → 1

[] → 0

[0,0] → 0

[2] → 2

[2,0,2] → 4

[10,9,8,9] → 11
JavaScript (Node.js), 38 bytes



a=&gt;a.map(v=&gt;(n+=v&gt;p&amp;&amp;v-p,p=v),p=n=0)|n


Try it online!

Simply a greedy algorithm which scan from left to right, only draw lines if needed, and draw it as long as possible.

Thanks Arnauld, save 2 3 bytes