Advertisement
STANAANDREY

soldiers_pb

Sep 7th, 2019
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solveSoldiersRow(vector<int> &heights) {
  5.     stack<int> soldiersStack;
  6.     for (int index = 0; index < (int)heights.size(); ++index) {
  7.         while (!soldiersStack.empty() && soldiersStack.top() <= heights[index]) {
  8.             soldiersStack.pop();
  9.         }
  10.         if (soldiersStack.empty()) {
  11.             // This soldier has clear visibility
  12.             cout << -1 << "\n";
  13.         } else {
  14.             // Found the corresponding pair
  15.             cout << soldiersStack.top() << "\n";
  16.         }
  17.         soldiersStack.push(heights[index]);
  18.     }
  19. }
  20.  
  21. int main()
  22. {
  23.     vector <int> v{5,3,1,4,9,2,7,5,2,4};
  24.     solveSoldiersRow(v);
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement