Chào mọi người, đây là đề bài:
Còn đây là bài làm của em.
#include "pch.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
void max_sliding_window_naive(vector<int> const & A, int w)
{
stack<int> inbox, outbox, maxbox;
for (int i = 0; i < A.size(); i++)
{
inbox.push(A[i]);
if (inbox.size() == w)
{
while (inbox.empty() == 0)
{
outbox.push(inbox.top());
if (maxbox.empty() == 1)
maxbox.push(inbox.top());
else if (inbox.top() > maxbox.top())
{
maxbox.push(inbox.top());
}
else
{
maxbox.push(maxbox.top());
}
inbox.pop();
}
outbox.pop();
cout << maxbox.top() << " ";
maxbox.pop();
while (outbox.empty() == 0)
{
inbox.push(outbox.top());
outbox.pop();
maxbox.pop();
}
}
}
return;
}
int main() {
int n = 0;
cin >> n;
vector<int> A(n);
for (size_t i = 0; i < n; ++i)
cin >> A.at(i);
int w = 0;
cin >> w;
max_sliding_window_naive(A, w);
return 0;
}
Bài làm của em hình như time complexity vẫn là O(nxm). Em không biết làm sao để nó hiệu quả hơn. Mọi người xem và gợi ý giúp em, em cảm ơn nhiều. :3