Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Codility - Stonewall problem: 100% on correctness but 0% on performance

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.

The problem

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.

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.

Write a function:

function solution(H);

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.

For example, given array H containing N = 9 integers: 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

the function should return 7. The figure shows one possible arrangement of seven blocks.

Write an efficient algorithm for the following assumptions:

    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].

My solution

function solution(H) {

    let stones = 0
    let absoluteMinimum = Infinity;
    let prevStones = []
    for (let i = 0; i < H.length; i++) {
        if (H[i] < 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
}

Here is the summary of my attempt including test results. https://app.codility.com/demo/results/training2V8Y42-AUQ/

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Following @Teemu's comment, and noting that the code simply is searching prevStones for the existence of a specific height, suggest creating another array that holds the counts of the heights in prevStonesCount.

Eg, if setting prevStones = [ 8 ] then set prevStonesCount[ 8 ] = 1, as there is one stone in the prevStones array of height 8.

Now, rather than having to perform prevStones.includes( 8 ), simply check if 0 < prevStonesCount[ 8 ]. (That is, .includes() searches the entire prevStones array for a height of 8, whereas prevStonesCount[ 8 ] in one step indicates whether any stones of height 8 are in the prevStones array.)

Thus, anytime performing a prevStones.push( x ) or prevStones.pop( x ), make the corresponding adjustment of prevStonesCount[ x ] += 1 or prevStonesCount[ x ] -= 1, respectively.

Note also that within the first if where prevStones = [H[i]], that the prevStones array is essentially cleared and set to an initial value. This means that the prevStonesCount array will also need to be cleared, and then set prevStonesCount[ H[i] ] = 1.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!