Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void solveSoldiersRow(vector<int> &heights) {
- stack<int> soldiersStack;
- for (int index = 0; index < (int)heights.size(); ++index) {
- while (!soldiersStack.empty() && soldiersStack.top() <= heights[index]) {
- soldiersStack.pop();
- }
- if (soldiersStack.empty()) {
- // This soldier has clear visibility
- cout << -1 << "\n";
- } else {
- // Found the corresponding pair
- cout << soldiersStack.top() << "\n";
- }
- soldiersStack.push(heights[index]);
- }
- }
- int main()
- {
- vector <int> v{5,3,1,4,9,2,7,5,2,4};
- solveSoldiersRow(v);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement