Created
February 7, 2020 10:31
-
-
Save jin-x/d3b708315cfaed1a006bacbfef5f53ed to your computer and use it in GitHub Desktop.
@jinxonik / UniLecs #207
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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