python3: easy solution
\n\n# Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n)\n Add your space complexity here, e.g. O(n) \n
\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# C++\n```\nclass Solution {\npublic:\n    vector<int> lastVisitedIntegers(vector<string>& words) {\n        vector<int> ans, num;\n        int k = 0, len;\n        for (auto word : words) {\n            if ( word == "prev") {\n                k += 1;\n                len = num.size();\n                if (k > len)\n                    ans.push_back(-1);\n                else\n                    ans.push_back(num[len-k]);\n            } else {\n                num.push_back(stoi(word));\n                k = 0;\n            }\n        }\n        return ans;\n    }\n};\n```\n\n# Python3\n```\nclass Solution:\n    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n        ans = []\n        num = []\n        k = 0\n        for each in words:\n            if each.isdigit():\n                num.append(int(each))\n                k = 0\n            else:\n                k += 1\n                if k > len(num):\n                    ans.append(-1)\n                else:\n                    ans.append(num[-k])\n        return ans\n```
['C++', 'Python3']