Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int lastRemaining(int n) {
  4.         if (n==0)
  5.             return 0;
  6.         std::vector<int> cnt(n);
  7.         std::iota(cnt.begin(), cnt.end(), 1);
  8.         std::list<int> stk (cnt.begin(),cnt.end());
  9.        
  10.         bool front=true;
  11.         while(stk.size()>1) {
  12.             if (front) {
  13.                 front=false;
  14.                 int numElems = stk.size();
  15.                 while(numElems-- > 0) {
  16.                     stk.pop_front();
  17.                     if (--numElems >= 0)
  18.                         stk.splice(stk.end(), stk, stk.begin());  
  19.                 }
  20.             }
  21.             else {
  22.                 front=true;
  23.                 int numElems = stk.size();
  24.                 while(numElems-- > 0) {
  25.                     stk.pop_back();
  26.                     if (--numElems >= 0)
  27.                         stk.splice(stk.begin(), stk, --stk.end());
  28.                 }
  29.             }
  30.         }
  31.         return stk.front();
  32.     }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement