const solution = (heights) => {
const stack = [];
const result = [];
const limit = heights.length;
while (stack.length < limit) {
const popped = heights.pop();
stack.push(popped);
let i;
for (i = heights.length - 1; i >= 0; i--) {
if (heights[i] > popped) {
result.unshift(i + 1);
break;
}
}
if (i < 0) {
result.unshift(0);
}
}
return result;
};