Optimizing Memory and Performance in Last Visited Integers: A Code Solution Exploration || Mr. Robot
Last Visited Integers\n\n## Approach 1: Tracking and Retrieving\n### Explanation\nIn this approach, we maintain two vectors, st to store the integers we encount
# Last Visited Integers\n\n## Approach 1: Tracking and Retrieving\n### Explanation\nIn this approach, we maintain two vectors, `st` to store the integers we encounter and `ans` to record the last visited integers when encountering the word "prev." We also keep track of consecutive "prev" words using the `conspre` variable to determine how many positions to go back in `st` when "prev" is encountered.\n\n### Dry Run\nLet\'s dry run the code with a sample input:\n```\nwords = ["3", "5", "prev", "prev", "1", "prev", "prev", "prev"]\n```\n- Initially, `st` and `ans` are empty.\n- Iterating through the words:\n  1. "3" is a number, so it\'s pushed onto `st`.\n  2. "5" is also a number and is pushed onto `st`.\n  3. "prev" is encountered. `conspre` becomes 1. We retrieve the last visited integer at position `st.size() - 1 - 1` (the first "prev") from `st`, which is 5, and push it onto `ans`.\n  4. The next "prev" is encountered, and `conspre` is now 2. We retrieve the last visited integer at position `st.size() - 2 - 1` (the second "prev") from `st`, which is 3, and push it onto `ans`.\n  5. "1" is a number and is pushed onto `st`.\n  6. Another "prev" is encountered, but there are not enough "prev" words to go back 3 positions in `st,` so we push -1 onto `ans`.\n  7. Similarly, the last "prev" also results in -1 being added to `ans`.\n\nThe final value of `ans` is `[5, 3, -1, -1]`.\n\n### Edge Cases\nThis approach handles the following edge cases:\n- Consecutive "prev" words: We track and retrieve the last visited integers accordingly.\n- "prev" words without previous numbers: We return -1 in these cases.\n\n### Complexity Analysis\n- Time Complexity: O(N), where N is the number of words. We iterate through the words once.\n- Space Complexity: O(N), where N is the number of words. We store the last visited integers in the `ans` vector.\n\n## Codes in (C++) (Java) (Python) (C#) (JavaScript)\n```cpp []\nclass Solution {\npublic:\n    vector<int> lastVisitedIntegers(vector<string>& words) {\n        vector<int> st;\n        vector<int> ans;\n        int conspre = 0;\n        for (auto i : words) {\n            if (i == "prev") {\n                conspre++;\n                if (conspre <= st.size()) {\n                    ans.push_back(st[st.size() - conspre]);\n                } else {\n                    ans.push_back(-1);\n                }\n            } else {\n                st.push_back(stoi(i));\n                conspre = 0;\n            }\n        }\n        return ans;\n    }\n};\n```\n\n### Java\n```java []\nimport java.util.ArrayList;\nimport java.util.List;\n\nclass Solution {\n    public List<Integer> lastVisitedIntegers(List<String> words) {\n        List<Integer> st = new ArrayList<>();\n        List<Integer> ans = new ArrayList<>();\n        int conspre = 0;\n        for (String word : words) {\n            if (word.equals("prev")) {\n                conspre++;\n                if (conspre <= st.size()) {\n                    ans.add(st.get(st.size() - conspre));\n                } else {\n                    ans.add(-1);\n                }\n            } else {\n                st.add(Integer.parseInt(word));\n                conspre = 0;\n            }\n        }\n        return ans;\n    }\n}\n```\n\n### Python\n```python []\nfrom typing import List\n\nclass Solution:\n    def lastVisitedIntegers(self, words: List[str]) -> List[int]:\n        st = []\n        ans = []\n        conspre = 0\n        for word in words:\n            if word == "prev":\n                conspre += 1\n                if conspre <= len(st):\n                    ans.append(st[-conspre])\n                else:\n                    ans.append(-1)\n            else:\n                st.append(int(word))\n                conspre = 0\n        return ans\n```\n\n### C#\n```csharp []\nusing System;\nusing System.Collections.Generic;\n\npublic class Solution {\n    public IList<int> LastVisitedIntegers(IList<string> words) {\n        List<int> st = new List<int>();\n        List<int> ans = new List<int>();\n        int conspre = 0;\n        foreach (string word in words) {\n            if (word == "prev") {\n                conspre++;\n                if (conspre <= st.Count) {\n                    ans.Add(st[st.Count - conspre]);\n                } else {\n                    ans.Add(-1);\n                }\n            } else {\n                st.Add(int.Parse(word));\n                conspre = 0;\n            }\n        }\n        return ans;\n    }\n}\n```\n\n### JavaScript\n```javascript []\n/**\n * @param {string[]} words\n * @return {number[]}\n */\nvar lastVisitedIntegers = function(words) {\n    const st = [];\n    const ans = [];\n    let conspre = 0;\n    for (const word of words) {\n        if (word === "prev") {\n            conspre++;\n            if (conspre <= st.length) {\n                ans.push(st[st.length - conspre]);\n            } else {\n                ans.push(-1);\n            }\n        } else {\n            st.push(parseInt(word));\n            conspre = 0;\n        }\n    }\n    return ans;\n};\n```    \n\n![image.png](https://assets.leetcode.com/users/images/2d346854-4b30-4b7c-8a7c-1d034519ee8e_1697307761.9712887.png)\n---\n\n| Language    | Runtime (ms) | Memory (MB) |\n|-------------|--------------|-------------|\n| C++         | 11           | 24          |\n| Java        | 2            | 44          |\n| Python      | 56           | 17          |\n| JavaScript  | 66           | 45          |\n| C#          | 123          | 47          |\n\n---\n\n\n\n---\n\n# Consider UPVOTING\u2B06\uFE0F\n\n![image.png](https://assets.leetcode.com/users/images/853344be-bb84-422b-bdec-6ad5f07d0a7f_1696956449.7358863.png)\n\n\n# DROP YOUR SUGGESTIONS IN THE COMMENT\n\n## Keep Coding\uD83E\uDDD1\u200D\uD83D\uDCBB\n\n -- *MR.ROBOT SIGNING OFF*\n
['Array', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']