LeetCode solutions

3164. Last Visited Integers

My accepted Python solution to LeetCode problem 3164, Last Visited Integers, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.1MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.1MB, accepted 2026-01-01.

python
class Solution:
    def lastVisitedIntegers(self, nums: List[int]) -> List[int]:
        seen = []
        ans = []
        consecutive = 0
        
        for num in nums:
            if num > 0:
                seen.append(num)
                consecutive = 0
            else:  # num == -1
                consecutive += 1
                k = consecutive
                if k <= len(seen):
                    ans.append(seen[-k])
                else:
                    ans.append(-1)
        
        return ans

Source