Easy O(N) solution
\n# Code\n\nclass Solution {\npublic:\n    vector<int> lastVisitedIntegers(vector<string>& words) {\n        //array to store the numbers encountered while trav
\n# Code\n```\nclass Solution {\npublic:\n    vector<int> lastVisitedIntegers(vector<string>& words) {\n        //array to store the numbers encountered while traversing\n        vector<int>numbers;\n        //An iterator for accessing the prev number\n        int i=-1 ;\n        vector<int>ans;\n        for(auto it: words){\n            string curr = it ; \n            if(curr=="prev"){\n                if(i==-1){\n                    // if no number comes before this prev store -1 \n                    ans.push_back(-1);\n                }\n                else{\n                    //adding the prev number to ans then decrementing i\n                    ans.push_back(numbers[i]);\n                    i--;\n                }\n            }\n            else{\n                // converting string to number and pushing it into numbers array \n                int num = stoi(curr);\n                numbers.push_back(num);\n                //updating i \n                i=numbers.size()-1;\n            }\n        }\n        return ans ;\n    }\n};\n```
['Array', 'C++']