Skip to content

Commit ed2b876

Browse files
solved in 15/12/23
1 parent 9a3aa47 commit ed2b876

File tree

3 files changed

+291
-0
lines changed

3 files changed

+291
-0
lines changed

LC_102_SlidingWindowMaximum.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// tc : O(n) sc : O(n)
5+
vector<int> maxSlidingWindow(vector<int> &nums, int k)
6+
{
7+
vector<int> ans;
8+
deque<int> q; // store the index of the maximum elements in the window
9+
// process first k elements - 1st window
10+
for (int i = 0; i < k; i++)
11+
{
12+
// remove the elements which are smaller than the current element because they can't be the maximum element in the window
13+
while (!q.empty() && nums[i] >= nums[q.back()])
14+
q.pop_back();
15+
q.push_back(i); // add the current element as it can be the maximum element in the window and the next window
16+
}
17+
// process remaining windows -> remove the element which is out of the window and add the new the element
18+
for (int i = k; i < nums.size(); i++)
19+
{
20+
ans.push_back(nums[q.front()]); // add the maximum element of the previous window in the answer array
21+
// remove the front element if it is out of the window
22+
if (i - q.front() >= k)
23+
q.pop_front();
24+
// remove the elements which are smaller than the current element because they can't be the maximum element in the window
25+
while (!q.empty() && nums[i] >= nums[q.back()])
26+
q.pop_back();
27+
q.push_back(i); // add the current element as it can be the maximum element in the window and the next window
28+
}
29+
// process the last window
30+
ans.push_back(nums[q.front()]);
31+
return ans;
32+
}

LC_103_DesignCircularQueue.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Time Complexity: O(1)
5+
// Space Complexity: O(N)
6+
class MyCircularQueue
7+
{
8+
public:
9+
MyCircularQueue(int k)
10+
{
11+
// the queue holding the elements for the circular queue
12+
q.resize(k);
13+
// the number of elements in the circular queue
14+
cnt = 0;
15+
// queue size
16+
sz = k;
17+
// the idx of the head element
18+
headIdx = 0;
19+
}
20+
21+
bool enQueue(int value)
22+
{
23+
// handle full case
24+
if (isFull())
25+
return false;
26+
// Given an array of size of 4, we can find the position to be inserted using the formula
27+
// targetIdx = (headIdx + cnt) % sz
28+
// e.g. [1, 2, 3, _]
29+
// headIdx = 0, cnt = 3, sz = 4, targetIdx = (0 + 3) % 4 = 3
30+
// e.g. [_, 2, 3, 4]
31+
// headIdx = 1, cnt = 3, sz = 4, targetIdx = (1 + 3) % 4 = 0
32+
q[(headIdx + cnt) % sz] = value;
33+
// increase the number of elements by 1
34+
cnt += 1;
35+
return true;
36+
}
37+
38+
bool deQueue()
39+
{
40+
// handle empty case
41+
if (isEmpty())
42+
return false;
43+
// update the head index
44+
headIdx = (headIdx + 1) % sz;
45+
// decrease the number of elements by 1
46+
cnt -= 1;
47+
return true;
48+
}
49+
50+
int Front()
51+
{
52+
// handle empty queue case
53+
if (isEmpty())
54+
return -1;
55+
// return the head element
56+
return q[headIdx];
57+
}
58+
59+
int Rear()
60+
{
61+
// handle empty queue case
62+
if (isEmpty())
63+
return -1;
64+
// Given an array of size of 4, we can find the tailIdx using the formula
65+
// tailIdx = (headIdx + cnt - 1) % sz
66+
// e.g. [0 1 2] 3
67+
// headIdx = 0, cnt = 3, sz = 4, tailIdx = (0 + 3 - 1) % 4 = 2
68+
// e.g. 0 [1 2 3]
69+
// headIdx = 1, cnt = 3, sz = 4, tailIdx = (1 + 3 - 1) % 4 = 3
70+
// e.g. 0] 1 [2 3
71+
// headIdx = 2, cnt = 3, sz = 4, tailIdx = (2 + 3 - 1) % 4 = 0
72+
return q[(headIdx + cnt - 1) % sz];
73+
}
74+
75+
bool isEmpty()
76+
{
77+
// no element in the queue
78+
return cnt == 0;
79+
}
80+
81+
bool isFull()
82+
{
83+
// return true if the count is equal to the queue size
84+
// else return false
85+
return cnt == sz;
86+
}
87+
88+
private:
89+
int cnt, sz, headIdx;
90+
vector<int> q;
91+
};
92+
93+
int main()
94+
{
95+
MyCircularQueue *obj = new MyCircularQueue(3);
96+
cout << obj->enQueue(1) << endl;
97+
cout << obj->enQueue(2) << endl;
98+
cout << obj->enQueue(3) << endl;
99+
cout << obj->enQueue(4) << endl;
100+
cout << obj->Rear() << endl;
101+
cout << obj->isFull() << endl;
102+
cout << obj->deQueue() << endl;
103+
cout << obj->enQueue(4) << endl;
104+
cout << obj->Rear() << endl;
105+
return 0;
106+
}

LC_104_DesignCircularDeque.cpp

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class MyCircularDeque
5+
{
6+
int size, front, rear, *arr;
7+
8+
public:
9+
MyCircularDeque(int k)
10+
{
11+
size = k;
12+
front = rear = -1;
13+
arr = new int[size];
14+
}
15+
16+
~MyCircularDeque()
17+
{
18+
delete[] arr;
19+
}
20+
21+
bool insertFront(int value)
22+
{
23+
if (isFull())
24+
return false;
25+
else if (isEmpty())
26+
{
27+
front++;
28+
rear++;
29+
arr[front] = value;
30+
}
31+
else if (front == 0 && rear != size - 1)
32+
{ // circular nature
33+
front = size - 1;
34+
arr[front] = value;
35+
}
36+
else
37+
{
38+
front--;
39+
arr[front] = value;
40+
}
41+
return true;
42+
}
43+
44+
bool insertLast(int value)
45+
{
46+
if (isFull())
47+
return false;
48+
else if (isEmpty())
49+
{
50+
front++;
51+
rear++;
52+
arr[rear] = value;
53+
}
54+
else if (rear == size - 1 && front != 0)
55+
{ // circular nature
56+
rear = 0;
57+
arr[rear] = value;
58+
}
59+
else
60+
{
61+
arr[++rear] = value;
62+
}
63+
return true;
64+
}
65+
66+
bool deleteFront()
67+
{
68+
if (isEmpty())
69+
return false;
70+
else if (front == rear)
71+
{ // single element
72+
arr[front] = -1;
73+
front = rear = -1;
74+
}
75+
else if (front == size - 1)
76+
{ // circular nature
77+
arr[front] = -1;
78+
front = 0;
79+
}
80+
else
81+
{
82+
arr[front] = -1;
83+
front++;
84+
}
85+
return true;
86+
}
87+
88+
bool deleteLast()
89+
{
90+
if (isEmpty())
91+
return false;
92+
else if (front == rear)
93+
{ // single element
94+
arr[rear] = -1;
95+
front = rear = -1;
96+
}
97+
else if (rear == 0)
98+
{ // circular nature
99+
rear = size - 1;
100+
}
101+
else
102+
{
103+
arr[rear] = -1;
104+
rear--;
105+
}
106+
return true;
107+
}
108+
109+
int getFront()
110+
{
111+
if (isEmpty())
112+
return -1;
113+
else
114+
{
115+
return arr[front];
116+
}
117+
}
118+
119+
int getRear()
120+
{
121+
if (isEmpty())
122+
return -1;
123+
else
124+
{
125+
return arr[rear];
126+
}
127+
}
128+
129+
bool isEmpty()
130+
{
131+
return (front == -1 && rear == -1);
132+
}
133+
134+
bool isFull()
135+
{
136+
return (front == 0 && rear == size - 1) || (rear == front - 1);
137+
}
138+
};
139+
140+
int main()
141+
{
142+
MyCircularDeque *obj = new MyCircularDeque(3);
143+
cout << obj->insertFront(1) << endl;
144+
cout << obj->insertLast(2) << endl;
145+
cout << obj->insertFront(3) << endl;
146+
cout << obj->insertLast(4) << endl;
147+
cout << obj->getRear() << endl;
148+
cout << obj->isFull() << endl;
149+
cout << obj->deleteLast() << endl;
150+
cout << obj->insertFront(4) << endl;
151+
cout << obj->getFront() << endl;
152+
return 0;
153+
}

0 commit comments

Comments
 (0)