aboutsummaryrefslogtreecommitdiff
path: root/challenge-345/eric-cheung/python/ch-2.py
blob: 5a4b728ca3d0dcbe0dfde26e15cd72ed23a668bd (plain)
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
## Ref.:
## https://leetcode.com/problems/last-visited-integers/description/

## arrInts = [5, -1, -1]  ## Example 1
## arrInts = [3, 7, -1, -1, -1]  ## Example 2
## arrInts = [2, -1, 4, -1, -1]  ## Example 3
## arrInts = [10, 20, -1, 30, -1, -1]  ## Example 4
## arrInts = [-1, -1, 5, -1]  ## Example 5

## arrInts = [1, 2, -1, -1, -1]  ## Example 6
arrInts = [1, -1, 2, -1, -1]  ## Example 7

arrSeen = []
arrAns = []
arrMinusOne = []

for nElem in arrInts:
    if nElem > 0:
        arrSeen.insert(0, nElem)
    else:
        arrAns.append(arrSeen[len(arrMinusOne)] if len(arrMinusOne) < len(arrSeen) else nElem)
        arrMinusOne.append(nElem)

print (arrAns)