Python Easy Solution | O(N) | 23ms | Beats 100% Users in Runtime & Memory
Intuition\nThe intuition behind this approach is to keep track of the integers visited using a stack, and when a "prev" word is encountered, determine the last 
# Intuition\nThe intuition behind this approach is to keep track of the integers visited using a stack, and when a "prev" word is encountered, determine the last visited integer based on the count of consecutive "prev" words and the total count of integers visited so far. If there are more "prev" words than integers on the stack, it appends -1 to the result list; otherwise, it retrieves the appropriate integer from the stack and appends it to the result list. This approach ensures that the last visited integers are correctly identified when encountering "prev" words in the input list.\n\n# Complexity\n- Time complexity:\nThis solution solves the problem in O(N) time \n\n- Space complexity:\nSpace Complecity of this code is O(N)\n\n# Code\n```\nclass Solution(object):\n    def lastVisitedIntegers(self, words):\n        """\n        :type words: List[str]\n        :rtype: List[int]\n        """\n        stack = []\n        res = []\n        total_visited_integers = 0\n        consecutive_prevs = 0\n        for word in words:\n            if word != "prev":\n                consecutive_prevs = 0\n                total_visited_integers += 1\n                stack.append(int(word))\n            else:\n                consecutive_prevs += 1\n                if consecutive_prevs > total_visited_integers:\n                    res.append(-1)\n                else:\n                    res.append(stack[-consecutive_prevs])\n                    \n        return res\n```
['Python']