-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLast Visited Integers
53 lines (40 loc) · 1.81 KB
/
Last Visited Integers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
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.
'''
#wrong solution
class Solution:
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
k = 0
res = []
nums_reverse = words[::-1]
for i in range(1,len(words)):
if words[i] == 'prev' and words[i-1]:
k+=1
elif words[i] == 'prev':
k = 1
if k > i:
res.append(-1)
else:
res.append(nums_reverse[k-1])
return res
-----------------------------------------------------------------------------------------------------------
class Solution:
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
k = 0
res = []
nums_reverse = []
for w in words:
if w.isdigit():
nums_reverse.append(int(w))
k = 0
else:
k+=1
if k> len(nums_reverse):
res.append(-1)
else:
res.append(nums_reverse[-k])
return res