Codility: StoneWall Solution
Lesson 7 Stacks and QueuesImplementation
1function Stack() {2 stack = []34 this.push = function (el) {5 stack.push(el)6 }78 this.pop = function () {9 return stack.pop()10 }1112 return {13 ...this,14 get size() {15 return stack.length16 },17 get head() {18 return stack[this.size - 1]19 },20 }21}2223function solution(H) {24 let stack = new Stack()25 let count = 026 H.forEach(h => {27 while (stack.size && stack.head > h) {28 stack.pop()29 }30 if (stack.size && stack.head === h) {31 return32 }33 stack.push(h)34 count++35 })36 return count37}
References
https://codility.com/media/train/solution-stone-wall.pdf
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: Nesting
Lesson 7 Stacks and Queues
Previous Post
Codility: Distinct
Lesson 6 Sorting