QUESTION

Text
Image


Your room is being decorated. On the largest wall you would like to paint a skyline. The skyline consists of rectangular buildings arranged in a line. The buildings are all of the same width, but they may have different heights. The skyline shape is given as an array $A$ whose elements specify the heights of consecutive buildings. For example, consider array A such that: \[ \begin{array}{l} A[0]=1 \\ A[1]=3 \\ A[2]=2 \\ A[3]=1 \\ A[4]=2 \\ A[5]=1 \\ A[6]=5 \\ A[7]=3 \\ A[8]=3 \\ A[9]=4 \\ A[10]=2 \end{array} \] The shape specified by this array is represented by the figure below.


You would like to paint the skyline using continuous horizontal brushstrokes. Every horizontal stroke is one unit high and arbitrarily wide. The goal is to calculate the minimum number of horizontal strokes needed. For example, the above shape can be painted using nine horizontal strokes. Starting from the bottom, you can paint the skyline in horizontal stripes with 1,3,2,2, 1 strokes per respective stripe. Write a function: \[ \text { class Solution \{ public int solution(int [] A); \} } \] that, given a non-empty array $\mathrm{A}$ consisting of $\mathrm{N}$ integers, returns the minimum number of horizontal brushstrokes needed to paint the


that, given a non-empty array $\mathrm{A}$ consisting of $\mathrm{N}$ integers, returns the minimum number of horizontal brushstrokes needed to paint the shape represented by the array. The function should return -1 if the number of strokes exceeds $1,000,000,000$. For example, given array A as described above, the function should return 9 , as explained above. On the other hand, for the following array $A$ : \[ \begin{array}{l} A[0]=5 \\ A[1]=8 \end{array} \] the function should return 8 , as you must paint one horizontal stroke at each height from 1 to 8. For the following array: \[ \begin{array}{l} A[0]=1 \\ A[1]=1 \\ A[2]=1 \\ A[3]=1 \end{array} \] the function should return 1 , as you can paint this shape using a single horizontal stroke.






For example, given array A as described above, the function should return 9 , as explained above. On the other hand, for the following array $\mathrm{A}$ : \[ \begin{array}{l} A[\theta]=5 \\ A[1]=8 \end{array} \] the function should return 8 , as you must paint one horizontal stroke at each height from 1 to 8. For the following array: \[ \begin{array}{l} A[0]=1 \\ A[1]=1 \\ A[2]=1 \\ A[3]=1 \end{array} \] the function should return 1 , as you can paint this shape using a single horizontal stroke. Write an efficient algorithm for the following assumptions: - $\mathrm{N}$ is an integer within the range [1..100,000]; - each element of array $A$ is an integer within the range $[1.1,000,000,000]$. Copyright 2009-2022 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

i want answer in java8 with return statement

Public Answer

AOZRDO The First Answerer