Last active
February 2, 2016 12:23
-
-
Save m4rr/debe7bea208a80d43e7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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