Simple Python Hashmaps
IntuitionWe make an initial pass through the list of nums. For any given number in the list, the distance has to be the minimum of:
the distance backwards in th
# Intuition
We make an initial pass through the list of nums. For any given number in the list, the distance has to be the minimum of:
1. the distance backwards in the array from the last instance of that number that we've seen. Any number before the last number is guaranteed to be farther going this direction.
2. The distance forwards (using the circular property of the list) going to the back and then the start of the list and reaching the first instance of the number that we've seen. Any number after the first number is guaranteed to be farther going this direction.

We maintain a map of indexes for both the first and last instances of our numbers and iterate through the list to find the minimum distance for each number. In each step, we check distances for (1) and (2), but since we have a new "last" number, we also need to update the distances of the first number, and the "previous last" number too.

# Code
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        n = len(nums)
        firsts = {}
        lasts = {}
        dists = [float("inf")]*n

        for i in range(n):
            if nums[i] not in firsts:
                firsts[nums[i]] = i
                lasts[nums[i]] = i
            else:
                dists[i] = min(i - lasts[nums[i]], n - i + firsts[nums[i]])
                dists[lasts[nums[i]]] = min(dists[lasts[nums[i]]], i - lasts[nums[i]])
                dists[firsts[nums[i]]] = min(dists[firsts[nums[i]]], n - i + firsts[nums[i]])
                lasts[nums[i]] = i
        
        res = []
        for q in queries:
            res.append(dists[q] if dists[q] != float('inf') else -1)

        return res


```
['Python3']