Daily iOS
0일 연속
CS 기초·알고리즘주관식Apple출처 leetcode.com

O(1) 최댓값 스택

편집기의 실행 취소 스택처럼 push/pop 되는 값 중 "현재 최댓값"을 O(1)에 돌려주는 스택을 만들려고 합니다. 먼저 Swift 프로토콜로 인터페이스를 정의하고, 구현 아이디어를 설명해 주세요.

protocol MaxStack {
    associatedtype Element: Comparable
    mutating func push(_ x: Element)
    mutating func pop() -> Element?
    func peekMax() -> Element?
}