Skip to content

Commit 734fc42

Browse files
solved on 16/12/23
1 parent ed2b876 commit 734fc42

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// tc : O(n) sc : O(n)
5+
long long sumOfMaxAndMin(vector<int> &nums, int n, int k)
6+
{
7+
long long ans = 0;
8+
deque<int> dq1, dq2; // store the index of the maximum and minimum 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 (!dq1.empty() && nums[i] >= nums[dq1.back()])
14+
dq1.pop_back();
15+
// remove the elements which are greater than the current element because they can't be the minimum element in the window
16+
while (!dq2.empty() && nums[i] <= nums[dq2.back()])
17+
dq2.pop_back();
18+
dq1.push_back(i); // add the current element as it can be the maximum element in the window and the next window
19+
dq2.push_back(i); // add the current element as it can be the minimum element in the window and the next window
20+
}
21+
ans += nums[dq1.front()] + nums[dq2.front()]; // add the maximum and minimum element of the previous window in the answer array
22+
23+
24+
// process remaining windows -> remove the element which is out of the window and add the new the element
25+
for (int i = k; i < nums.size(); i++)
26+
{
27+
// remove the front element if it is out of the window
28+
if (i - dq1.front() >= k) // if the maximum element is out of the window
29+
dq1.pop_front();
30+
if (i - dq2.front() >= k) // if the minimum element is out of the window
31+
dq2.pop_front();
32+
33+
// remove the elements which are smaller than the current element because they can't be the maximum element in the window
34+
while (!dq1.empty() && nums[i] >= nums[dq1.back()])
35+
dq1.pop_back();
36+
37+
// remove the elements which are greater than the current element because they can't be the minimum element in the window
38+
while (!dq2.empty() && nums[i] <= nums[dq2.back()])
39+
dq2.pop_back();
40+
dq1.push_back(i); // add the current element as it can be the maximum element in the window and the next window
41+
dq2.push_back(i); // add the current element as it can be the mimimum element in the window and the next window
42+
43+
ans += nums[dq1.front()] + nums[dq2.front()]; // find the ans for the current window
44+
}
45+
return ans;
46+
}
47+
48+
int main()
49+
{
50+
vector<int> nums = {1, 2, 3, 4, 5};
51+
int k = 3;
52+
int n = nums.size();
53+
cout << sumOfMaxAndMin(nums, n, k) << endl;
54+
return 0;
55+
}

LC_105_ImplementQueueUsingStacks.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class MyQueue
5+
{
6+
public:
7+
stack<int> s1, s2;
8+
MyQueue()
9+
{
10+
}
11+
12+
void push(int x)
13+
{
14+
s1.push(x);
15+
}
16+
17+
int pop()
18+
{
19+
int pop = -1;
20+
if (!s2.empty())
21+
{
22+
pop = s2.top();
23+
}
24+
else
25+
{
26+
while (!s1.empty())
27+
{
28+
s2.push(s1.top());
29+
s1.pop();
30+
}
31+
pop = s2.top();
32+
}
33+
s2.pop();
34+
return pop;
35+
}
36+
37+
int peek()
38+
{
39+
int peek = -1; // front element
40+
if (!s2.empty())
41+
{
42+
peek = s2.top();
43+
}
44+
else
45+
{
46+
while (!s1.empty())
47+
{
48+
s2.push(s1.top());
49+
s1.pop();
50+
}
51+
peek = s2.top();
52+
}
53+
return peek;
54+
}
55+
56+
bool empty()
57+
{
58+
return s1.empty() && s2.empty();
59+
}
60+
};
61+
62+
int main()
63+
{
64+
MyQueue *obj = new MyQueue();
65+
obj->push(1);
66+
obj->push(2);
67+
int param_2 = obj->pop();
68+
int param_3 = obj->peek();
69+
bool param_4 = obj->empty();
70+
cout << param_2 << endl;
71+
cout << param_3 << endl;
72+
cout << param_4 << endl;
73+
return 0;
74+
}

LC_106_ImplementStackUsingQueue.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class MyStack
5+
{
6+
public:
7+
queue<int> q;
8+
MyStack()
9+
{
10+
}
11+
12+
void push(int x)
13+
{
14+
q.push(x);
15+
for (int i = 0; i < q.size() - 1; i++)
16+
{
17+
q.push(q.front());
18+
q.pop();
19+
}
20+
}
21+
22+
int pop()
23+
{
24+
int pop = q.front();
25+
q.pop();
26+
return pop;
27+
}
28+
29+
int top()
30+
{
31+
return q.front();
32+
}
33+
34+
bool empty()
35+
{
36+
return q.empty();
37+
}
38+
};
39+
40+
int main()
41+
{
42+
MyStack *obj = new MyStack();
43+
obj->push(1);
44+
obj->push(2);
45+
cout << obj->top() << endl;
46+
cout << obj->pop() << endl;
47+
cout << obj->empty() << endl;
48+
return 0;
49+
}

0 commit comments

Comments
 (0)