Algorithm/programmers

뒤에 있는 큰 수 찾기 ( Level 2, JavaScript, 연습문제 )

takeU 2025. 1. 15. 22:48
반응형
const solution = (numbers) => {
    const stack = [[0, numbers[0]]], res = Array(numbers.length).fill(-1)
    for (let i = 1; i < numbers.length; i++) {
        while (stack.length && stack.at(-1)[1] < numbers[i]) {
            const [idx, cur] = stack.pop()
            res[idx] = numbers[i]
        }
        stack.push([i, numbers[i]])
    }
    return res
}

스택