Skip to content

Instantly share code, notes, and snippets.

@jin-x
Created February 7, 2020 10:31
Show Gist options
  • Save jin-x/d3b708315cfaed1a006bacbfef5f53ed to your computer and use it in GitHub Desktop.
Save jin-x/d3b708315cfaed1a006bacbfef5f53ed to your computer and use it in GitHub Desktop.
@jinxonik / UniLecs #207
#include <iostream>
#include <vector>
#include <stack>
using std::cout;
using std::vector;
using std::stack;
vector<int> emigration(const vector<int>& average)
{
int size = average.size();
vector<int> result(size, -1);
stack<int> buf;
for (int i = 0; i < size; ++i)
{
int n = average[i];
while (!buf.empty() && average[buf.top()] > n) {
result[buf.top()] = i;
buf.pop();
}
buf.push(i);
}
return result;
}
int main()
{
vector<int> x = emigration({ 1, 2, 3, 2, 1, 4, 2, 5, 3, 1 });
cout << "{ ";
for (const int n : x)
{
cout << n << " ";
}
cout << "}\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment