-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path132-pattern.go
77 lines (60 loc) · 1.31 KB
/
132-pattern.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
)
// source: https://leetcode.com/problems/132-pattern/
type stack struct {
values [][2]int
}
func newStack() stack {
return stack{}
}
func (s *stack) empty() bool {
return len(s.values) == 0
}
func (s *stack) push(val, curMin int) {
s.values = append(s.values, [2]int{val, curMin})
}
func (s *stack) pop() [2]int {
val := s.values[len(s.values)-1]
s.values = s.values[:len(s.values)-1]
return val
}
func (s *stack) top() [2]int {
return s.values[len(s.values)-1]
}
func find132pattern(nums []int) bool {
var s = newStack()
var curMin = nums[0]
min := func(x, y int) int {
if x < y {
return x
}
return y
}
for i := range nums {
for !s.empty() && s.top()[0] <= nums[i] {
s.pop()
}
if !s.empty() && s.top()[1] < nums[i] {
return true
}
s.push(nums[i], curMin)
curMin = min(nums[i], curMin)
}
return false
}
func main() {
// Example 4
var nums4 = []int{-2, 1, 1}
fmt.Println("Expected: false Output: ", find132pattern(nums4))
// Example 1
var nums1 = []int{1, 2, 3, 4}
fmt.Println("Expected: false Output: ", find132pattern(nums1))
// Example 2
var nums2 = []int{3, 1, 4, 2}
fmt.Println("Expected: true Output: ", find132pattern(nums2))
// Example 3
var nums3 = []int{-1, 3, 2, 0}
fmt.Println("Expected: true Output: ", find132pattern(nums3))
}