-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.cpp
256 lines (204 loc) · 4.67 KB
/
Queue.cpp
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
247
248
249
250
251
252
253
254
255
256
#include<bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
class Queue
{
int front;
int rear;
int size;
int *Q;
public:
Queue(){front=rear=-1;size=10;Q=new int[size];}
Queue(int size){front=rear=-1;this->size=size;Q=new int[this->size];}
void enqueue(int x);
int dequeue();
void Display();
};
void Queue::enqueue(int x)
{
if(rear == size-1)
cout<<"Queue Full"<<endl;
else
{
rear++;
Q[rear]=x;
}
}
int Queue::dequeue()
{
int x=-1;
if(front==rear)
cout<<"Queue Empty"<<endl;
else
{
x = Q[front+1];
front++;
}
return x;
}
void Queue::Display()
{
for(int i=front+1;i<=rear;i++)
cout<<Q[i]<<" ";
cout<<endl;
}
void modifyQ(queue<int>&q, int k) //Reverse First K elements of Queue
{
if(q.empty() || k<=0)
return ;
stack<int>stk;
for(int i=0;i<k;i++)
{
stk.push(q.front());
q.pop();
}
while(!stk.empty())
{
q.push(stk.top());
stk.pop();
}
for(int i=0;i<q.size()-k;i++)
{
q.push(q.front());
q.pop();
}
}
void Reverse(queue<int>&q)
{
if(q.empty())
return;
stack<int>stk;
while(!q.empty())
{
stk.push(q.front());
q.pop();
}
while(!stk.empty())
{
q.push(stk.top());
stk.pop();
}
}
void Interleave(queue<int>&q) //Interleave the first half of the queue with second half
{
if(q.size()%2!=0)
cout<<"Input even no. of elements"<<endl;
stack<int> stk;
int half = q.size()/2;
// Push first half elements into the stack
// queue:16 17 18 19 20, stack: 15(T) 14 13 12 11
for(int i=0;i<half;i++)
{
stk.push(q.front());
q.pop();
}
// enqueue back the stack elements
// queue: 16 17 18 19 20 15 14 13 12 11
while(!stk.empty())
{
q.push(stk.top());
stk.pop();
}
// dequeue the first half elements of queue
// and enqueue them back
// queue: 15 14 13 12 11 16 17 18 19 20
for(int i=0;i<half;i++)
{
q.push(q.front());
q.pop();
}
// Again push the first half elements into the stack
// queue: 16 17 18 19 20, stack: 11(T) 12 13 14 15
for(int i=0;i<half;i++)
{
stk.push(q.front());
q.pop();
}
// interleave the elements of queue and stack
// queue: 11 16 12 17 13 18 14 19 15 20
while(!stk.empty())
{
q.push(stk.top());
stk.pop();
q.push(q.front());
q.pop();
}
}
vector<int> printFirstNegativeInt(int arr[],int n, int k)
{
deque<int>di;
vector<int>res;
int i=0;
for(i=0;i<k;i++)
if(arr[i]<0)
di.push_back(i);
for(;i<n;i++)
{
if(!di.empty())
res.push_back(arr[di.front()]);
else
res.push_back(0);
while(!di.empty() && di.front() < (i-k+1))
di.pop_front();
if(arr[i]<0)
di.push_back(i);
}
if(!di.empty())
res.push_back(arr[di.front()]);
else
res.push_back(0);
return res;
}
int minSumSq(string S, int k)
{
int l = S.length();
if(k>=l)
return 0;
int freq[MAX_CHAR] = {0};
for(int i=0;i<l;i++)
freq[S[i]-'a']++;
priority_queue<int> q;
for(int i=0;i<MAX_CHAR;i++)
q.push(freq[i]);
while(k--)
{
int temp = q.top();
q.pop();
temp-=1;
q.push(temp);
}
int res=0;
while(!q.empty())
{
int temp = q.top();
res += temp*temp;
q.pop();
}
return res;
}
string FirstNonRepeating(string str)
{
queue<char> q;
int charCount[26] = { 0 };
string res;
for (int i = 0; str[i]; i++) {
q.push(str[i]);
charCount[str[i] - 'a']++;
while (!q.empty()) {
if (charCount[q.front() - 'a'] > 1)
q.pop();
else {
res.push_back(q.front());
break;
}
}
if (q.empty())
res.push_back('#');
}
return res;
}
int main()
{
string str = "aabc";
cout<< FirstNonRepeating(str);
}