Efficiently Determine the Last Visited Integers for Each -1 in an Array
Intuition\nThe goal is to track the most recent positive integers and use them to determine the output for each occurrence of -1 in the array. By maintaining a 
# Intuition\nThe goal is to track the most recent positive integers and use them to determine the output for each occurrence of -1 in the array. By maintaining a list of seen positive integers, we can efficiently find the required integer for each -1.\n\n# Approach\n1. Initialize Variables:\nn: the length of the array nums.\nk: a counter to track consecutive -1s.\nseen: a list to store the seen positive integers.\nans: a list to store the result for each -1.\n2. Iterate Through the Array:\nIf a positive integer is encountered, reset k to 0 and add the integer to seen.\nIf -1 is encountered, increment k.\nCheck if k is greater than the size of seen.\nIf true, append -1 to ans.\nIf false, append the element at position seen.size() - k to ans.\n3. Return the Result:\nReturn the ans list containing the results for each -1.\n\n# Complexity\n- Time complexity: O(n)\nWe traverse the array once, and operations with seen and ans lists are efficient.\n\n- Space complexity: O(n)\nWe use additional space to store the seen and ans lists, proportional to the input size.\n\n# Code\n```\nclass Solution {\n    public List<Integer> lastVisitedIntegers(int[] nums) {\n        int n=nums.length;\n        int k=0;\n\n        List<Integer> seen=new ArrayList<>();\n        List<Integer> ans=new ArrayList<>();\n\n        for(int i=0;i<n;i++){\n            if(nums[i]>0){\n                k=0;\n                seen.add(nums[i]);\n            }\n            else if(nums[i]==-1){\n                k++;\n                if(k>seen.size()){\n                    ans.add(-1);\n                }\n                else{\n                    ans.add(seen.get(seen.size()-k));\n                }\n            }\n        }\n\n        return ans;\n    }\n}\n```
['Java']