-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path00155-min_stack.go
More file actions
49 lines (38 loc) · 829 Bytes
/
00155-min_stack.go
File metadata and controls
49 lines (38 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 155: Min stack
// https://leetcode.com/problems/min-stack/
package main
import "fmt"
// SOLUTION
type MinStack struct {
s1, s2 []int
}
func (this *MinStack) push(x int) {
this.s1 = append(this.s1, x)
if len(this.s2)==0 || x <= this.getMin() {
this.s2 = append(this.s2, x)
}
}
func (this *MinStack) pop() {
if this.s1[len(this.s1)-1] == this.getMin() {
this.s2 = this.s2[:len(this.s2)-1]
}
this.s1 = this.s1[:len(this.s1)-1]
}
func (this *MinStack) top() int {
return this.s1[len(this.s1)-1]
}
func (this *MinStack) getMin() int {
return this.s2[len(this.s2)-1]
}
func main() {
var o MinStack
// OPERATIONS
o.push(-2)
o.push(0)
o.push(-3)
fmt.Print(o.getMin(), " ")
o.pop()
o.top()
fmt.Print(o.getMin(), " ")
fmt.Println()
}