Brute➡️Optimal | 2 Solutions | 🔥Intuitive - Self Explanatory Comments🔥 | C++ Clean Code✅
\uD83D\uDE0A ~ \uD835\uDE52\uD835\uDE5E\uD835\uDE69\uD835\uDE5D \u2764\uFE0F \uD835\uDE57\uD835\uDE6E \uD835\uDE43\uD835\uDE5E\uD835\uDE67\uD835\uDE5A\uD835\uDE
\uD83D\uDE0A ~ \uD835\uDE52\uD835\uDE5E\uD835\uDE69\uD835\uDE5D \u2764\uFE0F \uD835\uDE57\uD835\uDE6E \uD835\uDE43\uD835\uDE5E\uD835\uDE67\uD835\uDE5A\uD835\uDE63\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n***Hey There! It\'s Easy As Heaven, Just Take A Look At The Code And Comments Within It, You\'ll Get It.\nStill Have Doubts! Feel Free To Comment, I\'ll Definitely Reply!***\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n***Two Solutions : Both Accepted***\n\n# Complexity\n- *Time complexity: Mentioned in the code*\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- *Space complexity: Mentioned in the code*\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n**Solution 1 :**\n```\n// #1 Method to find the last visited integers - O(N*N) & O(N)   \nvector<int> lastVisitedIntegers(vector<string>& words) {\n    vector<int> output; // Stores the last visited integers (resultant values).\n    vector<int> nums;   // Stores the integers seen so far, in reverse order.\n\n    // Tracks the number of consecutive "prev" strings encountered up to and including the current string.\n    int k = 0; \n    \n    for(int i=0; i<words.size(); i++) {\n        // If the ith string is a digit, then insert it as a digit at the beginning of the "nums" array. \n        if(words[i] != "prev") {\n           nums.insert(begin(nums), stoi(words[i]));          \n        }\n        // Else when the ith string is "prev".\n        else {\n            // If the "prev" strings is seen consecutively, then increase the consecutive count by 1.\n            if(i > 0 && words[i-1] == "prev") {\n                k += 1;\n            }\n            // Else when the "prev" strings is not seen consecutively, reset the consecutive count by 1.\n            else {\n                k = 1;\n            }\n            \n            // If the (k-1) is a valid index, then store the index element of "nums" to "output".\n            if(nums.size() > 0 && k-1 < nums.size()) {\n                output.push_back(nums[k-1]);\n            }\n            // If the (k-1) is not a valid index, then store the value -1 to the "output".\n            else {\n                output.push_back(-1);\n            }\n        }   \n    }\n    \n    // Return the array containing the last visited integers.\n    return output;\n}\n```\n**Solution 2 :**\n```\n// #2 Method to find the last visited integers - O(N) & O(N)        \nvector<int> lastVisitedIntegers(vector<string>& words) {\n    vector<int> output; // Stores the resultant values.\n    vector<int> nums;   // Stores the integers seen so far, in original order.\n\n    // Tracks the number of consecutive "prev" strings encountered up to and including the current string.\n    int k = 0; \n    \n    for(int i=0; i<words.size(); i++) {\n        // If the ith string is a digit, then insert it as a digit to the "nums" array. \n        if(words[i] != "prev") {\n           nums.push_back(stoi(words[i]));          \n        }\n        // Else when the ith string is "prev".\n        else {\n            // If the "prev" strings is seen consecutively, then increase the consecutive count by 1.\n            if(i > 0 && words[i-1] == "prev") {\n                k += 1;\n            }\n            // Else when the "prev" strings is not seen consecutively, reset the consecutive count by 1.\n            else {\n                k = 1;\n            }\n            \n            // If the (k-1) is a valid index, then store the index element of "nums" to "output".\n            if(nums.size() > 0 && k-1 < nums.size()) {\n                // Assume the index element is in the reverse of the "nums", then calculate the index based on the reverse array.\n                int idx = nums.size() - k;\n                // If the calcuated index is valid, then store the index element to the "output". \n                if(idx >= 0 && idx < nums.size())\n                    output.push_back(nums[idx]);\n            }\n            // If the (k-1) is not a valid index, then store the value -1 to the "output".\n            else {\n                output.push_back(-1);\n            }\n        }\n    }\n    \n    // Return the array containing the last visited integers.\n    return output;\n}\n```\n\uD835\uDDE8\uD835\uDDE3\uD835\uDDE9\uD835\uDDE2\uD835\uDDE7\uD835\uDDD8 \uD835\uDDDC\uD835\uDDD9 \uD835\uDDEC\uD835\uDDE2\uD835\uDDE8 \uD835\uDDDF\uD835\uDDDC\uD835\uDDDE\uD835\uDDD8 \uD835\uDDE7\uD835\uDDDB\uD835\uDDD8 \uD835\uDDE6\uD835\uDDE2\uD835\uDDDF\uD835\uDDE8\uD835\uDDE7\uD835\uDDDC\uD835\uDDE2\uD835\uDDE1 \uD83D\uDC4D
['Array', 'String', 'C++']