Stack
A stack is a LIFO structure: the last value pushed is the first value popped.
이 멘탈 모델부터 시작하세요
A stack is a LIFO structure: the last value pushed is the first value popped.
불필요한 전문용어 없이 동작 방식 이해하기
Think of a stack of plates. You add and remove from one end only.
Core operations are push, pop and peek/top. With a good implementation they are O(1).
알고리즘을 연속된 의사결정으로 읽기
Push adds to the top.
Pop removes and returns the top.
Peek reads the top without removing it.
push(1), push(2), pop() returns 2.
class Stack {
#items = [];
push(value) { this.#items.push(value); }
pop() { return this.#items.pop(); }
peek() { return this.#items.at(-1); }
get size() { return this.#items.length; }
}문법을 외우지 마세요. 각 줄에서 바뀌는 상태를 따라가고 위에서 설명한 불변식과 연결하세요.
push/pop O(1)
Do not quote this complexity mechanically. Ask which operation or input dimension it describes, then derive the cost from the loops, recursion depth, data-structure operations and any preprocessing assumptions.
적합한 경우
- Undo/redo
- Expression parsing
- DFS and backtracking
보통 어디에서 문제가 생기는가
- Popping from an empty stack without handling it.
- Using Array.shift() and accidentally making operations O(n).
전문적인 답변 구조
Say LIFO first, then name push/pop/peek and one real use such as call stacks or parsing.
What problem does Stack solve?
A stack is a LIFO structure: the last value pushed is the first value popped.
복잡도에 대해 무엇을 언급해야 하나요?
Its typical cost here is push/pop O(1), but a complete answer should connect that cost to the representation, input constraints and the dominant operations.
가장 흔한 실수는 무엇인가요?
Popping from an empty stack without handling it.