Algorithms
Easy
2899
3164
Last Visited Integers
last-visited-integers
"""
Given a 0-indexed array of strings words where words[i] is either a positive integer represented as a string or the string "prev".

Start iterating from the beginning of the array; for every "prev" string seen in words, find the last visited integer in words which is defined as follows:

 * Let k be the number of consecutive "prev" strings seen so far (containing the current string). Let nums be the 0-indexed array of integers seen so far and nums_reverse be the reverse of nums, then the integer at (k - 1)th index of nums_reverse will be the last visited integer for this "prev".
 * If k is greater than the total visited integers, then the last visited integer will be -1.

Return an integer array containing the last visited integers.

Example 1:

Input: words = ["1","2","prev","prev","prev"]
Output: [2,1,-1]
Explanation:
For "prev" at index = 2, last visited integer will be 2 as here the number of consecutive "prev" strings is 1, and in the array reverse_nums, 2 will be the first element.
For "prev" at index = 3, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
For "prev" at index = 4, last visited integer will be -1 as there are a total of three consecutive "prev" strings including this "prev" which are visited, but the total number of integers visited is two.

Example 2:

Input: words = ["1","prev","2","prev","prev"]
Output: [1,2,1]
Explanation:
For "prev" at index = 1, last visited integer will be 1.
For "prev" at index = 3, last visited integer will be 2.
For "prev" at index = 4, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.


Constraints:

 * 1 <= words.length <= 100
 * words[i] == "prev" or 1 <= int(words[i]) <= 100
"""
class Solution:
    def lastVisitedIntegers(self, words: List[str]) -> List[int]:
        
Given a 0-indexed array of strings words where words[i] is either a positive integer represented as a string or the string "prev".

Start iterating from the beginning of the array; for every "prev" string seen in words, find the last visited integer in words which is defined as follows:

 * Let k be the number of consecutive "prev" strings seen so far (containing the current string). Let nums be the 0-indexed array of integers seen so far and nums_reverse be the reverse of nums, then the integer at (k - 1)th index of nums_reverse will be the last visited integer for this "prev".
 * If k is greater than the total visited integers, then the last visited integer will be -1.

Return an integer array containing the last visited integers.

Example 1:

Input: words = ["1","2","prev","prev","prev"]
Output: [2,1,-1]
Explanation:
For "prev" at index = 2, last visited integer will be 2 as here the number of consecutive "prev" strings is 1, and in the array reverse_nums, 2 will be the first element.
For "prev" at index = 3, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
For "prev" at index = 4, last visited integer will be -1 as there are a total of three consecutive "prev" strings including this "prev" which are visited, but the total number of integers visited is two.

Example 2:

Input: words = ["1","prev","2","prev","prev"]
Output: [1,2,1]
Explanation:
For "prev" at index = 1, last visited integer will be 1.
For "prev" at index = 3, last visited integer will be 2.
For "prev" at index = 4, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.


Constraints:

 * 1 <= words.length <= 100
 * words[i] == "prev" or 1 <= int(words[i]) <= 100


Please complete the code below to solve above prblem:
```python
class Solution:
    def lastVisitedIntegers(self, words: List[str]) -> List[int]:
        
```

my_solution = Solution()

test_input = { "words": ["1","2","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [2,1,-1]

test_input = { "words": ["1","prev","2","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [1,2,1]

test_input = { "words": ["prev","prev","prev","27"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1]

test_input = { "words": ["17","42"] }
assert my_solution.lastVisitedIntegers(**test_input) == []

test_input = { "words": ["prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]

test_input = { "words": ["prev","prev","prev","52","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,52]

test_input = { "words": ["prev","prev","68","prev","prev","53","40","23","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,68,-1,23]

test_input = { "words": ["99","23","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [23]

test_input = { "words": ["prev","prev","prev","58","99","prev","10","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,99,10]

test_input = { "words": ["prev","51","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,51,-1]

test_input = { "words": ["prev","46","9","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,9]

test_input = { "words": ["prev","prev","prev","prev","prev","26"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,-1,-1]

test_input = { "words": ["prev","21","prev","76","82","prev","96","prev","57","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,21,82,96,57]

test_input = { "words": ["52","4","prev","prev","prev","69"] }
assert my_solution.lastVisitedIntegers(**test_input) == [4,52,-1]

test_input = { "words": ["24","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [24]

test_input = { "words": ["46","prev","78","prev","83","21","prev","94","50"] }
assert my_solution.lastVisitedIntegers(**test_input) == [46,78,21]

test_input = { "words": ["14","66","prev","prev","46","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [66,14,46]

test_input = { "words": ["35","90"] }
assert my_solution.lastVisitedIntegers(**test_input) == []

test_input = { "words": ["prev","9","prev","8","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,9,8]

test_input = { "words": ["prev","prev","88","71","47","65","24","39"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]

test_input = { "words": ["45","73","78","2","54","prev","85","62","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [54,62]

test_input = { "words": ["prev","prev","80","9","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,9]

test_input = { "words": ["79","19","prev","prev","prev","67","prev","16","2"] }
assert my_solution.lastVisitedIntegers(**test_input) == [19,79,-1,67]

test_input = { "words": ["94","prev","prev","prev","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [94,-1,-1,-1,-1,-1]

test_input = { "words": ["prev","prev","prev","82","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,82]

test_input = { "words": ["94","14","81","43","prev","43","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [43,43]

test_input = { "words": ["prev","prev","94","56","prev","32","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,56,32,56,94]

test_input = { "words": ["93"] }
assert my_solution.lastVisitedIntegers(**test_input) == []

test_input = { "words": ["46","91","3","40","31","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [31]

test_input = { "words": ["41","prev","17","58","78"] }
assert my_solution.lastVisitedIntegers(**test_input) == [41]

test_input = { "words": ["prev","prev","82","41","96","89","71"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]

test_input = { "words": ["4","prev","50","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [4,50,4]

test_input = { "words": ["59","76","prev","29","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [76,29]

test_input = { "words": ["prev","62"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]

test_input = { "words": ["6","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [6]

test_input = { "words": ["prev","prev","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,-1,-1]

test_input = { "words": ["28","5","35","prev","41","27","70","65","84"] }
assert my_solution.lastVisitedIntegers(**test_input) == [35]

test_input = { "words": ["94","45","prev","61"] }
assert my_solution.lastVisitedIntegers(**test_input) == [45]

test_input = { "words": ["prev","34","prev","prev","prev","prev","21","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,34,-1,-1,-1,21]

test_input = { "words": ["prev","12","100","33","prev","85","93"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,33]

test_input = { "words": ["26"] }
assert my_solution.lastVisitedIntegers(**test_input) == []

test_input = { "words": ["27","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [27,-1,-1]

test_input = { "words": ["prev","prev","22","33","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,33,22,-1]

test_input = { "words": ["30","prev","87","prev","19","prev","8","prev","81"] }
assert my_solution.lastVisitedIntegers(**test_input) == [30,87,19,8]

test_input = { "words": ["35","prev","47","82","86","84","prev","76","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [35,84,76]

test_input = { "words": ["prev","87"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]

test_input = { "words": ["prev","69","78","prev","prev","16"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,78,69]

test_input = { "words": ["22","97","prev","2"] }
assert my_solution.lastVisitedIntegers(**test_input) == [97]

test_input = { "words": ["72","74"] }
assert my_solution.lastVisitedIntegers(**test_input) == []

test_input = { "words": ["84","prev","prev","21"] }
assert my_solution.lastVisitedIntegers(**test_input) == [84,-1]

test_input = { "words": ["64","24"] }
assert my_solution.lastVisitedIntegers(**test_input) == []

test_input = { "words": ["17","prev","59","prev","51","11","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [17,59,11,51]

test_input = { "words": ["57","prev","27","30","prev","prev","75"] }
assert my_solution.lastVisitedIntegers(**test_input) == [57,30,27]

test_input = { "words": ["65","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [65,-1]

test_input = { "words": ["prev","53","76","54","94","77"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]

test_input = { "words": ["89","51","prev","prev","12","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [51,89,12,51]

test_input = { "words": ["prev","28","25","prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,25,28,-1]

test_input = { "words": ["51","prev","prev","76"] }
assert my_solution.lastVisitedIntegers(**test_input) == [51,-1]

test_input = { "words": ["2","24","63","prev","43","19","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [63,19]

test_input = { "words": ["prev","38","1"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1]

test_input = { "words": ["56","75","prev","prev","94"] }
assert my_solution.lastVisitedIntegers(**test_input) == [75,56]

test_input = { "words": ["prev","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1]

test_input = { "words": ["prev","37","25","31","prev","prev","42"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,31,25]

test_input = { "words": ["73","30","prev","20","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [30,20,30]

test_input = { "words": ["85","prev","prev","78","prev","100","8","17","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [85,-1,78,17]

test_input = { "words": ["prev","55","prev","87","19","prev","13","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,55,19,13,19]

test_input = { "words": ["prev","prev","5","prev","prev","prev","80","17"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,5,-1,-1]

test_input = { "words": ["100","3","prev","prev","93","35","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [3,100,35,93]

test_input = { "words": ["75","7"] }
assert my_solution.lastVisitedIntegers(**test_input) == []

test_input = { "words": ["prev","prev","prev","prev","prev","prev","71","prev","27"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,-1,-1,-1,71]

test_input = { "words": ["prev","prev","prev","91","44","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,44]

test_input = { "words": ["prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]

test_input = { "words": ["11","prev","87","prev","prev","94","prev","68"] }
assert my_solution.lastVisitedIntegers(**test_input) == [11,87,11,94]

test_input = { "words": ["78"] }
assert my_solution.lastVisitedIntegers(**test_input) == []

test_input = { "words": ["prev","prev","73","prev","prev","27","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,73,-1,27]

test_input = { "words": ["prev","70","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,70,-1]

test_input = { "words": ["68","prev","38","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [68,38,68]

test_input = { "words": ["prev","prev","36"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]

test_input = { "words": ["prev","prev","36","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,36]

test_input = { "words": ["18","58","41","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [41,58]

test_input = { "words": ["prev","prev","35"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1]

test_input = { "words": ["prev","72","prev","96","9","50","prev","52"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,72,50]

test_input = { "words": ["92","95","47","48","prev","50","34","prev","prev","46"] }
assert my_solution.lastVisitedIntegers(**test_input) == [48,34,50]

test_input = { "words": ["36","88","15","99","48"] }
assert my_solution.lastVisitedIntegers(**test_input) == []

test_input = { "words": ["93","prev","2","58","83","90","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [93,90]

test_input = { "words": ["prev","68","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,68]

test_input = { "words": ["prev","56","prev","prev","36","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,56,-1,36]

test_input = { "words": ["53","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [53]

test_input = { "words": ["5","29","94","3","48","prev","59","90","prev","69"] }
assert my_solution.lastVisitedIntegers(**test_input) == [48,90]

test_input = { "words": ["89","prev","prev","75","prev","98","80","prev","68","33"] }
assert my_solution.lastVisitedIntegers(**test_input) == [89,-1,75,80]

test_input = { "words": ["prev","93","prev","74","33"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,93]

test_input = { "words": ["prev","prev","10","25","prev","54","prev","prev","prev","76"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,25,54,25,10]

test_input = { "words": ["9","prev","14","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [9,14]

test_input = { "words": ["prev","prev","prev","18","66","92","prev","87","85","25"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,92]

test_input = { "words": ["prev","prev","prev","16","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,-1,16]

test_input = { "words": ["prev","prev","99","prev","82","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,99,82,99]

test_input = { "words": ["prev","5","90","71","prev","prev","61"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,71,90]

test_input = { "words": ["prev","prev","71","54","prev","20","65","prev","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,54,65,20]

test_input = { "words": ["prev","85","prev","93","prev","98","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,85,93,98]

test_input = { "words": ["prev","prev","34","prev"] }
assert my_solution.lastVisitedIntegers(**test_input) == [-1,-1,34]