Skip to content

Instantly share code, notes, and snippets.

@sghall
Created January 23, 2017 07:30
Show Gist options
  • Save sghall/42eb3644db8ae48d2ea045569ec56c4a to your computer and use it in GitHub Desktop.
Save sghall/42eb3644db8ae48d2ea045569ec56c4a to your computer and use it in GitHub Desktop.
Count Rectangles in Wall
function reactanlgeCount(arr) {
const stack = [];
let count = 0;
for (let i = 0; i < arr.length; i++) {
const next = arr[i];
while (stack.length && next < stack[stack.length - 1]) {
stack.pop();
}
if (!(stack.length && stack[stack.length - 1] === next)) {
stack.push(next);
count++;
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment