-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily103.cpp
More file actions
246 lines (199 loc) · 5.01 KB
/
Copy pathdaily103.cpp
File metadata and controls
246 lines (199 loc) · 5.01 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Solution 1 - FAIL
class MyCircularDeque {
public:
MyCircularDeque(int k)
: max_size_{k},
curr_size_{0},
dq_{},
head_{std::make_pair(-1, -1)},
tail_{std::make_pair(-1, -1)},
empty_{true},
full_{false} {
dq_ = std::vector<int>(k, -1);
}
bool insertFront(int value) {
return insert_value(true, value);
}
bool insertLast(int value) {
return insert_value(false, value);
}
bool deleteFront() {
return delete_value(true);
}
bool deleteLast() {
return delete_value(false);
}
int getFront() {
return head_.second;
}
int getRear() {
return tail_.second;
}
bool isEmpty() {
return empty_;
}
bool isFull() {
return full_;
}
private:
bool insert_value(bool front, int value) {
if (isFull())
return false;
if (curr_size_ == 0) {
head_ = std::make_pair(0, value);
tail_ = std::make_pair(0, value);
}
auto current = value;
auto index = head_.first;
int dir = 1;
if (!front) {
index = tail_.first;
dir = -1;
}
for (auto i = 0; i < max_size_; ++i) {
auto temp = dq_[index];
if (temp != -1) {
dq_[index] = value;
current = temp;
}
index = (index + dir + max_size_) % max_size_;
}
empty_ = false;
curr_size_++;
if (curr_size_ == max_size_)
full_ = true;
return true;
}
bool delete_value(bool front) {
if (isEmpty())
return false;
auto index = head_.first;
int dir = 1;
dq_[index] = -1;
if (!front) {
index = tail_.first;
dir = -1;
}
index = (index + dir + max_size_) % max_size_;
for (auto i = 0; i < max_size_; ++i) {
if (dq_[index] == -1) {
index = (index + dir + max_size_) % max_size_;
continue;
}
break;
}
if (front) {
head_.first = index;
head_.second = dq_[index];
} else {
tail_.first = index;
tail_.second = dq_[index];
}
curr_size_--;
if (curr_size_ == 0) {
head_ = tail_ = std::make_pair(-1, -1);
empty_ = true;
}
if (isFull())
full_ = false;
return true;
}
int max_size_;
int curr_size_;
std::vector<int> dq_;
std::pair<int, int> head_;
std::pair<int, int> tail_;
bool empty_;
bool full_;
};
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque* obj = new MyCircularDeque(k);
* bool param_1 = obj->insertFront(value);
* bool param_2 = obj->insertLast(value);
* bool param_3 = obj->deleteFront();
* bool param_4 = obj->deleteLast();
* int param_5 = obj->getFront();
* int param_6 = obj->getRear();
* bool param_7 = obj->isEmpty();
* bool param_8 = obj->isFull();
*/
// Solution 2
class MyCircularDeque {
public:
MyCircularDeque(int k)
: max_size_{k},
curr_size_{0},
dq_(k, -1),
head_{0},
tail_{k - 1}, // Initialize tail to the last index
empty_{true},
full_{false} {
}
bool insertFront(int value) {
return insert_value(true, value);
}
bool insertLast(int value) {
return insert_value(false, value);
}
bool deleteFront() {
return delete_value(true);
}
bool deleteLast() {
return delete_value(false);
}
int getFront() {
return isEmpty() ? -1 : dq_[head_];
}
int getRear() {
return isEmpty() ? -1 : dq_[tail_];
}
bool isEmpty() {
return empty_;
}
bool isFull() {
return full_;
}
private:
bool insert_value(bool front, int value) {
if (isFull())
return false;
if (front) {
head_ = (head_ - 1 + max_size_) % max_size_;
dq_[head_] = value;
} else {
tail_ = (tail_ + 1) % max_size_;
dq_[tail_] = value;
}
empty_ = false;
curr_size_++;
full_ = (curr_size_ == max_size_);
return true;
}
bool delete_value(bool front) {
if (isEmpty())
return false;
if (front) {
dq_[head_] = -1;
head_ = (head_ + 1) % max_size_;
} else {
dq_[tail_] = -1;
tail_ = (tail_ - 1 + max_size_) % max_size_;
}
curr_size_--;
empty_ = (curr_size_ == 0);
if (empty_) {
head_ = 0;
tail_ = max_size_ - 1;
}
full_ = false;
return true;
}
int max_size_;
int curr_size_;
std::vector<int> dq_;
int head_;
int tail_;
bool empty_;
bool full_;
};