-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCR184-DesignCheckoutSystem.go
More file actions
155 lines (137 loc) · 3.84 KB
/
LCR184-DesignCheckoutSystem.go
File metadata and controls
155 lines (137 loc) · 3.84 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
// LCR 184. 设计自助结算系统
// 请设计一个自助结账系统,该系统需要通过一个队列来模拟顾客通过购物车的结算过程,需要实现的功能有:
// get_max():获取结算商品中的最高价格,如果队列为空,则返回 -1
// add(value):将价格为 value 的商品加入待结算商品队列的尾部
// remove():移除第一个待结算的商品价格,如果队列为空,则返回 -1
// 注意,为保证该系统运转高效性,以上函数的均摊时间复杂度均为 O(1)
// 示例 1:
// 输入:
// ["Checkout","add","add","get_max","remove","get_max"]
// [[],[4],[7],[],[],[]]
// 输出: [null,null,null,7,4,7]
// 示例 2:
// 输入:
// ["Checkout","remove","get_max"]
// [[],[],[]]
// 输出: [null,-1,-1]
// 提示:
// 1 <= get_max, add, remove 的总操作数 <= 10000
// 1 <= value <= 10^5
import "fmt"
type Checkout struct {
queue []int
deque []int // 单调递减,保持第0个始终为最大值
}
func Constructor() Checkout {
return Checkout{}
}
func (this *Checkout) Get_max() int {
if len(this.deque) == 0 {
return -1
}
return this.deque[0]
}
func (this *Checkout) Add(value int) {
this.queue = append(this.queue, value)
for len(this.deque) > 0 && this.deque[len(this.deque)-1] < value {
this.deque = this.deque[:len(this.deque)-1]
}
this.deque = append(this.deque, value)
}
func (this *Checkout) Remove() int {
if len(this.queue) == 0 {
return -1
}
x := this.queue[0]
this.queue = this.queue[1:]
if x == this.deque[0] {
this.deque = this.deque[1:]
}
return x
}
type Checkout1 struct {
maxPrice int
maxPriceIndex int
queue []int
}
func Constructor1() Checkout1 {
queue := make([]int, 0)
return Checkout1{
maxPrice: -1,
maxPriceIndex: -1,
queue: queue,
}
}
func (this *Checkout1) Get_max() int {
if len(this.queue) == 0 {
return -1
}
return this.queue[this.maxPriceIndex]
}
func (this *Checkout1) Add(value int) {
if value > this.maxPrice {
this.maxPrice = value
this.maxPriceIndex = len(this.queue)
}
this.queue = append(this.queue, value)
}
func (this *Checkout1) Remove() int {
if len(this.queue) == 0 {
return -1
}
res := this.queue[0]
this.queue = this.queue[1:]
this.maxPriceIndex--
if this.maxPriceIndex < 0 {
if len(this.queue) > 0 {
maxPriceIndex, maxPrice := 0, this.queue[0]
for i := 1; i < len(this.queue); i++ {
if this.queue[i] > maxPrice {
maxPrice = this.queue[i]
maxPriceIndex = i
}
}
this.maxPrice = maxPrice
this.maxPriceIndex = maxPriceIndex
} else {
this.maxPrice = -1
}
}
return res
}
func main() {
// 示例 1:
// 输入:
// ["Checkout","add","add","get_max","remove","get_max"]
// [[],[4],[7],[],[],[]]
// 输出: [null,null,null,7,4,7]
obj1 := Constructor()
obj1.Add(4)
fmt.Println(obj1)
obj1.Add(7)
fmt.Println(obj1)
fmt.Println(obj1.Get_max()) // 7
fmt.Println(obj1.Remove()) // 4
fmt.Println(obj1.Get_max()) // 7
// 示例 2:
// 输入:
// ["Checkout","remove","get_max"]
// [[],[],[]]
// 输出: [null,-1,-1]
obj2 := Constructor()
fmt.Println(obj2.Remove()) // -1
fmt.Println(obj2.Get_max()) // -1
obj11 := Constructor1()
obj11.Add(4)
fmt.Println(obj11)
obj11.Add(7)
fmt.Println(obj11)
fmt.Println(obj11.Get_max()) // 7
fmt.Println(obj11.Remove()) // 4
fmt.Println(obj11.Get_max()) // 7
obj12 := Constructor1()
fmt.Println(obj12)
fmt.Println(obj12.Remove()) // -1
fmt.Println(obj12.Get_max()) // -1
}