KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • 백준
  • js
  • Level 0
  • tree
  • 그래프
  • codeup
  • recursion
  • C++
  • Level1
  • programmers
  • 문자열
  • Level 1
  • LeetCode
  • string
  • 다이나믹 프로그래밍
  • 제코베 JS 100제
  • 정렬
  • Level 2
  • 수학
  • C

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Design / 155번 / Min Stack / JS

2023. 5. 14. 19:30

< 문제 바로가기 >

 

Min Stack - LeetCode

Can you solve this real interview question? Min Stack - 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 t

leetcode.com

 

< 문제 간단설명 >

stack의 메소드들을 구현하는데 추가로 getMin()을 구현한다. getMin()은 스택의 값중에서 가장 작은값을 반환한다. 그 외 기본 메소드는 push, pop, top을 구현한다.

 

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

/** 
 * @param {number} val
 * @return {void}
 */
MinStack.prototype.push = function(val) {
  this.stack.push(val);

  if(this.minStack.length === 0) {
    this.minStack.push(val);
  }
  else {
    if(val < this.minStack.at(-1)) {
      this.minStack.push(val);
    }
    else {
      this.minStack.push(this.minStack.at(-1));
    }
  }
};

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

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

/**
 * @return {number}
 */
MinStack.prototype.getMin = function() {
  return this.minStack.at(-1);
};

/** 
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(val)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.getMin()
 */
저작자표시 (새창열림)

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

LeetCode / Matrix / 2352번 / Equal Row and Column Pairs / JS  (0) 2023.06.17
LeetCode / Array / 228번 / Summary Ranges / JS  (0) 2023.06.17
LeetCode / Design / 232번 / Implement Queue using Stacks / JS  (0) 2023.05.14
LeetCode / Tree / 199번 / Binary Tree Right Side View / JS  (0) 2023.05.14
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Matrix / 2352번 / Equal Row and Column Pairs / JS
    • LeetCode / Array / 228번 / Summary Ranges / JS
    • LeetCode / Design / 232번 / Implement Queue using Stacks / JS
    • LeetCode / Tree / 199번 / Binary Tree Right Side View / JS
    KimMinJun
    KimMinJun

    티스토리툴바