Skip to content

Instantly share code, notes, and snippets.

@m4rr
Last active February 2, 2016 12:23
Show Gist options
  • Save m4rr/debe7bea208a80d43e7d to your computer and use it in GitHub Desktop.
Save m4rr/debe7bea208a80d43e7d to your computer and use it in GitHub Desktop.
class Stack {
typealias Element = Int
private var storage: [Element] = []
private var minimumList: [Element] = []
func push(item: Element) {
let minOfCurrentAndLast = min(item, minimum() ?? item)
minimumList.append(minOfCurrentAndLast)
storage.append(item)
}
func pop() -> Element? {
guard !storage.isEmpty else {
return nil
}
minimumList.removeLast()
return storage.removeLast()
}
func minimum() -> Element? {
return minimumList.last
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment