O(n) | | C++  🔥
Complexity

Time complexity:
O(n)


Space complexity:
O(n)

Code
![Screenshot 2025-01-24 at 1.33.54 PM.png](https://assets.leetcode.com/users/images/1277dacd-659e-4fc4-a03d-e4caa6cf0da4_1737705982.5795968.png)

# Complexity
- Time complexity:
$$O(n)$$

- Space complexity:
$$O(n)$$

# Code
```cpp []
class Solution {
public:
    vector<int> lastVisitedIntegers(vector<int>& nums) {
        vector<int> seen, ans;
        int k = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] > 0) {
                // reset k to 0
                k = 0;

                // prepend element in front of array
                reverse(seen.begin(), seen.end());
                seen.push_back(nums[i]);
                reverse(seen.begin(), seen.end());

            } else if (nums[i] == -1) {
                k += 1;

                if (k <= seen.size()) {
                    ans.push_back(seen[k - 1]);
                } else {
                    ans.push_back(-1);
                }
            }
        }
        return ans;
    }
};
```
['Array', 'Math', 'C++']