g | x | w | all
Bytes Lang Time Link
007Uiua240819T133058ZjanMakos
007Vyxal240818T012032Zlyxal
045Vyxal s240818T011318Zlyxal
011Uiua230929T191905Zchunes
005Thunno 2 S230614T050524ZThe Thon
005Nekomata230614T035451Zalephalp
033Arturo230210T081050Zchunes
006Japt x190204T130334ZShaggy
nan220503T122231ZDr.jacky
011Dyalog APL220406T072355ZBrian BE
024Factor + math.unicode220406T083242Zchunes
004Husk210321T030342ZLeo
087Common Lisp190207T002726ZCharlim
032Haskell190204T144613ZWheat Wi
057Java JDK190206T081808ZOlivier
050Clojure190206T062236ZTried Cr
034Wolfram Language Mathematica190206T045109Zalephalp
008Stax190205T194741Zwastl
008Pyth190205T110540ZSok
030R190204T125104ZdigEmAll
047C# Visual C# Interactive Compiler with flag /uSystem.Math190204T195929ZGymhgy
032Haskell190205T023934Zxnor
021Perl 5190204T142408ZNahuel F
007K oK190204T115851ZGalen Iv
035Haskell190204T154437Zlynn
00505AB1E190204T121134ZArnauld
038JavaScript Node.js190204T114723Ztsh
037Python 3190204T114027ZTFeld
005Jelly190204T130832ZArnauld
021Retina 0.8.2190204T130451ZNeil
008MATL190204T123847ZSanchise
008Japt190204T115607ZLuis fel
01005AB1E190204T115804ZKevin Cr
013MATL190204T115657ZLuis Men

Uiua, 7 bytes

/+↥0°\+

Try it online!

Explanation

/+↥0°\+

    °\+ # Get deltas (inverse of prefix sum)
  ↥0 # Max with 0 (remove negatives)
/+ # Sum

Vyxal, 7 bytes

×*§Ġ~ȧL

Try it Online!

Unlike all the other answers, this sort of visually solves the problem. Let me show you how:

Explained

×*§Ġ~ȧL­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢​‎⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌­
×*       # ‎⁡Push n asterisks for each n in the input
  §      # ‎⁢The magic. Vertically join the list (that is, transpose, join on newlines, filling with spaces)
   Ġ     # ‎⁣Group by consecutive characters
    ~ȧ   # ‎⁤And remove those which are just whitespace
      L  # ‎⁢⁡Get the length of that list
💎

Created with the help of Luminespire.

Given an input of [1,3,2,1,2,1,5,3,3,4,2], it first becomes:

["*", "***", "**", "*", "**", "*", "*****", "***", "***", "****", "**"]

Then, we create the skyline:

      *    
      *  * 
 *    **** 
 ** * *****
***********

From here, we can find each unique cluster of asterisks.

      (*)    
      (*)  (*) 
 (*)    (****) 
 (**) (*) (*****)
(***********)

(The answer doesn't insert the brackets, but it does do the grouping part). Count the number of clusters, and you get the required answer.

Vyxal s, 36 bitsv2, 4.5 bytes

0p¯~Ṡ

Try it Online!

Bitstring:

001111110100000100010101001101100110

Boring algorithm everyone else uses. Bleh

Uiua, 11 bytes

/+↥0-↘¯1⊂0.

Try it!

Port of tsh's JavaScript answer.

/+↥0-↘¯1⊂0.
          .  # duplicate
        ⊂0   # prepend a zero
     ↘¯1     # drop last element
    -        # subtract the two arrays
  ↥0         # change negative numbers to zero
/+           # sum

Thunno 2 S, 6 5 bytes

0ƤṢ0§

Attempt This Online!

Port of @Arnauld's 05AB1E answer.
-1 thanks to @alephalpha

Explanation

0ƤṢ0§   # Implicit input 
0Ƥ      # Prepend a 0
  Ṣ     # Compute deltas
   0§   # Maximum with 0
        # S flag sums the list
        # Implicit output

Nekomata, 5 bytes

ç-P‼∑

Attempt This Online!

A port of @Arnauld's 05AB1E answer.

ç-P‼∑
ç           Prepend zero
 -          Minus to get the delta
  P‼        Filter by positive
    ∑       Sum

Arturo, 35 33 bytes

$=>[l:0sum map&'x[max@[0x-l]l:x]]

Try it

Japt -x, 7 6 bytes

änT f¬

Try it

1 byte saved thanks to Oliver.

änT f¬     :Implicit input of integer array
ä          :Consecutive pairs
 n         :Reduced by inverse subtraction
  T        :After first prepending 0
    f      :Filter elements by
     ¬     :Square root (The square root of a negative being NaN, which is falsey)
           :Implicitly reduce by addition and output

Kotlin

fun f(test:IntArray):Int{var s=0;var p=0;test.forEach{val f=(if(it>p)p else it);p=it;s-=f-p};return s}

Try it yourself

Dyalog APL, 11 bytes

+/0⌈¯2-/0,⊢

, concatenate, so 0, concatenates 0.

¯2-/
or
¯2 length of window. (minus means the window is rotated)
-/ does a window operation of -(minus) with window size 2

0⌈ the operation is called ceiling, but when it gets an input from both sides it also does "get the larger of the two inputs" so 0⌈1 3 0 ¯2 will give 1 3 0 0.

and finally, a +/ which is a sum.

Try it!

Factor + math.unicode, 24 bytes

[ dup 0 prefix [v-] Σ ]

Try it online!

Explanation

         ! { 10 9 8 9 }
dup      ! { 10 9 8 9 } { 10 9 8 9 }
0        ! { 10 9 8 9 } { 10 9 8 9 } 0
prefix   ! { 10 9 8 9 } { 0 10 9 8 9 }
[v-]     ! { 10 0 0 1 }  (subtract two vectors, clamping negative numbers to 0)
Σ        ! 11

Husk, 4 bytes

ΣẊ>Θ

Try it online!

Uses the same algorithm as everybody else (see here for an explanation), it just does so with fewer bytes than anybody else :D

Explanation

ΣẊ>Θ
   Θ    Prepend a 0 to the values
 Ẋ      For each pair of adjacent values (a,b)
  >     Return b-a if b>a else 0
Σ       Sum all of these values

> in Husk does a great job for this task: it does return a truthy value iff b>a, but this truthy value also happens to be the difference b-a, accomplishing two tasks in one go!

Common Lisp, 88 87 bytes

(lambda(s)(let((o 0))(dolist(c s)(incf o(max 0 c))(mapl(lambda(y)(decf(car y)c))s))o))

non-minified

(lambda (skyline)
  (let ((output 0))
    (dolist (current-skyscraper-height skyline)
      (incf output (max 0 current-skyscraper-height))
      (mapl (lambda (skyscraper)
              (decf (car skyscraper) current-skyscraper-height))
            skyline))
    output)))

Test it

When one tower is painted, it takes a number of brushstrokes equal to it's height. These brushstrokes translate to all the following ones, which is indicated here by subtracting the current tower's height from all other towers (and itself, but that doesn't matter). If a following tower is shorter, then it will be pushed to a negative number, and this negative number will subsequently subtracted from the towers that follow (indicating brushstrokes that could not be translated from a previous tower to the next ones). It actually just subtracts the number from all tower heights, including previous ones, but this doesn't matter because we don't look at the previous ones again.

Haskell, 34 32 bytes

2 bytes trimmed by Lynn

g x=sum$max 0<$>zipWith(-)x(0:x)

Try it online!

So to start we have zipWith(-). This takes two lists and produces a new list of their pairwise differences. We then combine it with x and (0:x). (0:) is a function that adds zero to the front of a list and by combining it with zipWith(-) we get the differences between consecutive elements of that list with a zero at the front. Then we turn all the negative ones to zero with (max 0<$>). This creates a new list where each element is the number of new strokes that has to be started at each tower. To get the total we just sum these with sum.

Java (JDK), 57 bytes

a->{int s=0,p=0;for(int x:a)s-=(x>p?p:x)-(p=x);return s;}

Try it online!

Another port of tsh's Javascript answer. So make sure you've upvoted them.

Note that I used subtraction instead of addition because it allowed me to write (p=x) as right operand in the same statement.

Clojure, 50 bytes

#((reduce(fn[[s n]i][(+(max(- i n)0)s)i])[0 0]%)0)

Try it online! (Why doesn't this print anything?)

#( ; begin anonymous function
    (reduce
        (fn [[s n] i] ; internal anonymous reducing function, destructures accumulator argument into a sum and the previous item
            [(+ (max (- i n) 0) s ; the sum part of the accumulator becomes the previous sum plus the larger of zero and the difference between the current number and the last one, which is how many new strokes need to be started at this point
            i]) ; ...and the previous item part becomes the current item
        [0 0] ; the initial value of the accumulator gives no strokes yet, and nothing for them to cover yet
        %) ; reduce over the argument to the function
    0) ; and get the sum element of the last value of the accumulator.

Wolfram Language (Mathematica), 34 bytes

A port of @Arnauld's solution.

Tr@Select[{##,0}-{0,##},#>0&]&@@#&

Try it online!

Stax, 8 bytes

Φ┐Γ╟φφ.╨

Run and debug it

Uses the widely used approach from tsh's JavaScript solution.

Pyth, 8 bytes

s>#0.++0

Yet another port of @tsh's marvellous answer. Takes the sum (s) of the positive values (>#0) of the deltas (.+) of the input with 0 prepended (+0Q, trailing Q inferred).

Try it online here, or verify all the test cases at once here.

String joining method, 10 bytes

This was the solution I wrote before browsing the other answers.

lcj.t+d*LN

Test suite.

lcj.t+d*LNQ   Implicit: Q=eval(input()), b=<newline>, N=<quote mark>
              Trailing Q inferred
        L Q   Map each element of Q...
       * N    ... to N repeated that many times
     +b       Prepend a newline
   .t         Transpose, padding with spaces
  j           Join on newlines
 c            Split on whitespace
l             Take the length, implicit print

R, 30 bytes

sum(pmax(0,diff(c(0,scan()))))

Try it online!

Porting of @Arnauld solution which in turn derives from @tsh great solution

C# (Visual C# Interactive Compiler) with flag /u:System.Math, 47 bytes

n=>n.Select((a,i)=>i<1?a:Max(a-n[i-1],0)).Sum()

Try it online!

Old version, with flag /u:System.Math, 63 bytes

n=>n.Aggregate((0,0),(a,b)=>(a.Item1+Max(0,b-a.Item2),b)).Item1

I feel like this solution is more elegant than the first one. It goes through the array with a two-value tuple as a starting value, picking up values, and stores the value before it in the second part of the tuple.

Try it online!

Haskell, 32 bytes

(0%)
p%(h:t)=max(h-p)0+h%t
p%_=0

Try it online!

An improvement on Lynn's solution that tracks the previous element p instead of looking at the next element. This makes the base case and recursive call shorter in exchange for needing to invoke (0%).

max(h-p)0 could be max h p-p for the same length.

Perl 5, 21 bytes

$\+=$_>$'&&$_-$';//}{

TIO

How

K (oK), 12 7 bytes

-5 bytes thanks to ngn!

A k (oK) port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):

+/0|-':

Try it online!

J, 15 bytes

A J port of Arnauld's 05AB1E solution (and tsh's JavaScript solution):

1#.0>./2-~/\0,]

Try it online!

My naive solution:

J, 27 bytes

1#.2(1 0-:])\0,@|:@,~1$~"0]

Try it online!

Haskell, 35 bytes

f(a:b:c)=max(a-b)0+f(b:c)
f a=sum a

Try it online!

Line 2 could be f[a]=a if I didn't also have to handle the case [].

05AB1E,  8 7  5 bytes

Saved 2 bytes thanks to @Adnan

0š¥þO

Try it online!

How?

This is using the algorithm that was first found by @tsh. If you like this answer, make sure to upvote their answer as well!

Each time a skyscraper is lower than or as high as the previous one, it can be painted 'for free' by simply extending the brushstrokes.

For instance, painting skyscrapers \$B\$ and \$C\$ in the figure below costs nothing.

On the other hand, we need 2 new brushstrokes to paint skyscraper \$E\$, no matter if they're going to be reused after that or not.

buildings

For the first skyscraper, we always need as many brushstrokes as there are floors in it.

Turning this into maths:

$$S=h_0+\sum_{i=1}^n \max(h_i-h_{i-1},0)$$

If we prepend \$0\$ to the list, this can be simplified to:

$$S=\sum_{i=1}^n \max(h_i-h_{i-1},0)$$

Commented

0š¥þO     # expects a list of non-negative integers  e.g. [10, 9, 8, 9]
0š        # prepend 0 to the list                    -->  [0, 10, 9, 8, 9]
  ¥       # compute deltas                           -->  [10, -1, -1, 1]
   þ      # keep only values made of decimal digits
          # (i.e. without a minus sign)              -->  ["10", "1"]
    O     # sum                                      -->  11

JavaScript (Node.js), 38 bytes

a=>a.map(v=>(n+=v>p&&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

Python 3, 37 bytes

lambda a:sum(a)-sum(map(min,a[1:],a))

Try it online!

-5 bytes by switching to Python 3, thanks to Sarien


Python 2, 47 43 42 bytes

lambda a:sum(a)-sum(map(min,a[1:],a[:-1]))

Try it online!

Alt:

lambda a:sum(a)-sum(map(min,zip(a[1:],a)))

Try it online!

Jelly, 5 bytes

A port of my 05AB1E answer, which itself is similar to @tsh JS answer.

ŻI0»S

Try it online!

Commented

ŻI0»S    - main link, expecting a list of non-negative integers  e.g. [10, 9, 8, 9]
Ż        - prepend 0                                             -->  [0, 10, 9, 8, 9]
 I       - compute the deltas                                    -->  [10, -1, -1, 1]
  0»     - compute max(0, v) for each term v                     -->  [10, 0, 0, 1]
    S    - sum                                                   -->  11

Retina 0.8.2, 21 bytes

\d+
$*
(1+)(?=,\1)

1

Try it online! Link includes test cases. Explanation:

\d+
$*

Convert to unary.

(1+)(?=,\1)

Delete all overlaps with the next tower, which don't need a new stroke.

1

Count the remaining strokes.

MATL, 8 bytes

0whd3Y%s

Try it online!

Pretty much @Arnauld's algorithm. Saved one byte (thanks @LuisMendo) by casting to uint64 rather than selecting ) positive entries.

Japt, 8 bytes

-2 bytes from @Shaggy

mîT Õ¸¸è

Explanation

mîT Õ¸¸è      Full program. Implicit input U
                e.g. U = [2,0,2]
mîT             Map each item X and repeat T(0) X times
                     U = ["00","","00"]
    Õ           Transpose rows with columns
                     U = ["0 0","0 0"]
     ¸¸         Join using space and then split in space
                     U = ["0","0","0","0"]
        è       Return the count of the truthy values

Try it online!

05AB1E, 13 10 bytes

Z>Lε@γPO}O

Try it online or verify all test cases.

Explanation:

Z            # Get the maximum of the (implicit) input-list
 >           # Increase it by 1 (if the list only contains 0s)
  L          # Create a list in the range [1, max]
   ε         # Map each value to:
    @        #  Check if this value is >= for each value in the (implicit) input
     γ       #  Split into chunks of adjacent equal digits
      P      #  Take the product of each inner list
       O     #  Take the sum
        }O   # And after the map: take the sum (which is output implicitly)

MATL, 15 14 13 bytes

ts:<~"@Y'x]vs

Input is a column vector, using ; as separator.

Try it online! Or verify all test cases.

Explanation

t       % Implicit input: column vector. Duplicate
s       % Sum
:       % Range from 1 to that. Gives a row vector
<~      % Greater or equal? Element-wise with broadcast
"       % For each column
  @     %   Push current columnn
  Y'    %   Run-length encoding. Gives vector of values (0, 1) and vector of lengths
  x     %   Delete vector of lengths
]       % End
v       % Vertically concatenate. May give an empty array
s       % Sum. Implicit display