[PS] Leet | 155. 최솟값 스택

2025. 8. 19. 15:59·CS/PS

문제

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Implement the MinStack class:

  • MinStack() initializes the stack object.
  • void push(int val) pushes the element val onto the stack.
  • void pop() removes the element on the top of the stack.
  • int top() gets the top element of the stack.
  • int getMin() retrieves the minimum element in the stack.

You must implement a solution with O(1) time complexity for each function.

예시

Input

["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

 

Output

[null,null,null,null,-3,null,0,-2]

 

Explanation

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top();    // return 0
minStack.getMin(); // return -2

조건

  • -231 <= val <= 231 - 1
  • Methods pop, top and getMin operations will always be called on non-empty stacks.
  • At most 3 * 104 calls will be made to push, pop, top, and getMin.

답

 두 개의 스택을 함께 사용합니다.

 

첫 번째 스택 stack은 일반적인 값을 담는 용도로,

 

두 번째 스택 minStack은 첫 번째 스택에 값들이 담겼을 때의 최솟값을 기록하는 용도로 사용합니다.

 

하여 stack에 값이 들어갈 때마다 그 때의 최솟값을 minStack에 저장하고,

 

반대로 값이 나갈 때마다는 minStack에서도 값을 빼주면 됩니다.

MinStack( )

var MinStack = function() {
    this.stack = [];
    this.minStack = [];
};

MinStack.push( )

/** 
 * @param {number} val
 * @return {void}
 */
MinStack.prototype.push = function(val) {
    this.stack.push(val);
    if (this.minStack.length === 0) {
        this.minStack.push(val);
    }
    else {
        const curMin = this.minStack[this.minStack.length - 1];
        this.minStack.push((curMin > val)? val : curMin);
    }
};

MinStack.pop( )

/**
 * @return {void}
 */
MinStack.prototype.pop = function() {
    this.minStack.pop()
    return this.stack.pop()
};

MinStack.top( )

/**
 * @return {number}
 */
MinStack.prototype.top = function() {
    return this.stack[this.stack.length-1]
};

MinStack.getMin( )

/**
 * @return {number}
 */
MinStack.prototype.getMin = function() {
    return this.minStack[this.minStack.length-1]
};
728x90

'CS > PS' 카테고리의 다른 글

[PS] NYPC | 2515. 잃어버린 섬 여행  (0) 2025.09.17
[PS] NYPC | 2513. 등차수열  (0) 2025.09.17
[PS] Leet | 146. LRU 캐시  (1) 2025.08.18
[PS] Leet | 295. 데이터 스트림에서 중간값 찾기  (2) 2025.08.06
[PS] Leet | 212. 단어 찾기 II  (2) 2025.07.25
'CS/PS' 카테고리의 다른 글
  • [PS] NYPC | 2515. 잃어버린 섬 여행
  • [PS] NYPC | 2513. 등차수열
  • [PS] Leet | 146. LRU 캐시
  • [PS] Leet | 295. 데이터 스트림에서 중간값 찾기
Rayi
Rayi
  • Rayi
    아카이브
    Rayi
  • 전체
    오늘
    어제
    • 분류 전체보기 (262)
      • CS (40)
        • ML (3)
        • CV (2)
        • PS (34)
      • Reveiw (17)
        • Paper (17)
        • Github (0)
      • Pytorch (5)
      • Language (58)
        • Python (7)
        • JavaScript (32)
        • TypeScript (16)
        • C++ (3)
      • IDE (12)
      • Git (13)
      • Frontend (71)
        • React (8)
        • SolidJS (20)
        • CSS (12)
      • UI (3)
      • Backend (15)
        • DB (17)
        • Node.js (11)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    API
    react
    CS
    PRISMA
    Git
    figma
    ML
    review
    ts
    Express
    vscode
    mongo
    python
    js
    PyTorch
    backend
    GAN
    nodejs
    DB
    Three
    ps
    UI
    html
    postgresql
    frontend
    C++
    CV
    deploy
    SOLID
    CSS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
Rayi
[PS] Leet | 155. 최솟값 스택
상단으로

티스토리툴바