Codility: Peaks Solution
Lesson 10 Prime and Composite NumbersImplementation
1function solution(A) {2 const N = A.length3 const isPeak = (a, b, c) => a < b && b > c45 const peaks = []6 for (let i = 1; i < N - 1; i++) {7 if (isPeak(A[i - 1], A[i], A[i + 1])) {8 peaks.push(i)9 }10 }1112 for (let blockSize = 1; blockSize <= N; blockSize++) {13 if (N % blockSize !== 0) {14 continue15 }16 let quotient = 017 const numberOfBlocks = Math.floor(N / blockSize)18 let valid = true19 for (const peakIndex of peaks) {20 if (Math.floor(peakIndex / blockSize) === quotient) {21 quotient++22 }23 }2425 // make sure each block have at least 1 peak26 if (quotient !== numberOfBlocks) {27 valid = false28 }29 if (valid) return numberOfBlocks30 }31 return 032}
References
https://rafal.io/posts/codility-peaks.html
Comments
Loading comments...
Tags
codility
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Codility: MinPerimeterRectangle
Lesson 10 Prime and Composite Numbers