Simple C++ Solution || C++ || Beats 100%
IntuitionThe problem requires us to maintain a list of recently visited positive integers and retrieve specific elements based on the number of consecutive -1s
# Intuition  
The problem requires us to maintain a list of recently visited positive integers and retrieve specific elements based on the number of consecutive `-1`s encountered. By storing positive integers in a data structure and tracking the count of consecutive `-1`s, we can efficiently determine the last visited integer.  

---

# Approach  
- Use a `list<int>` (`li`) to store positive integers in reverse order (most recent at the front).  
- Maintain a variable `k` to track the count of consecutive `-1`s.  
- Iterate through `nums`:  
  - If `nums[i]` is positive, add it to the front of `li`.  
  - If `nums[i]` is `-1`:  
    - If it's consecutive to a previous `-1`, increment `k`.  
    - Otherwise, reset `k` to `1`.  
    - If `k` is within the bounds of `li`, retrieve the `k-th` most recent integer; otherwise, return `-1`.  
    
---

# Complexity  
- **Time complexity:** \(O(n)\), since each element is processed once, and `advance(it, k-1)` runs in \(O(k)\) in the worst case but is overall efficient.  
- **Space complexity:** \(O(n)\), as we store up to `n` elements in `li`.
---

# Code
```cpp []
class Solution {
public:
    vector<int> lastVisitedIntegers(vector<int>& nums) {
        int prev=-1;
        vector<int>ans;
        list<int>li;
        int k=0;
        for(int i=0;i<nums.size();i++){
            if(nums[i]!=-1){
                li.push_front(nums[i]);
            }
            else{
                if(prev==-1||prev+1==i){
                    k++;
                    if(k>li.size()){
                        ans.push_back(-1);
                    }
                    else{
                        list<int>:: iterator it =li.begin();
                        advance(it,k-1);
                        ans.push_back(*it);
                    }
                    prev=i;
                }
                else{
                    k=1;
                    list<int>::iterator it =li.begin();
                    advance(it,k-1);
                    ans.push_back(*it);
                    prev=i;
                }
            }
        }
        return ans;
    }
};
```
['C++']