Optimized Code: Beats 100%
IntuitionApproachComplexity
Time complexity: O(n)

Space complexity: O(n)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    vector<int> lastVisitedIntegers(vector<int>& nums) {
        deque<int> seen; // Using deque to enable O(1) insertions at the front
        vector<int> ans;  // This will hold the result

        int consecutiveNegatives = 0; // Count consecutive -1s

        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] != -1) {
                // If the current element is a positive integer, prepend it to seen
                seen.push_front(nums[i]);  // O(1) operation
                consecutiveNegatives = 0;  // Reset consecutive -1s count after encountering a positive integer
            } else {
                consecutiveNegatives++;  // Increment consecutive -1s count
                // Append the corresponding last visited integer or -1 if not enough elements in seen
                if (consecutiveNegatives <= seen.size()) {
                    ans.push_back(seen[consecutiveNegatives - 1]);
                } else {
                    ans.push_back(-1);
                }
            }
        }
        return ans;
    }
};
```
['C++']