Python 3 🐍 || Beats 100% 🚀 || Easy 🔥
IntuitionThe problem requires finding the closest valid point that shares either the same x or y coordinate with our current location. Since Manhattan distance
![Todayyyyy.png](https://assets.leetcode.com/users/images/cb05b1fe-2c79-4678-87cb-9e1a1688470d_1739090733.1835186.png)


# Intuition  
The problem requires finding the closest valid point that shares either the same x or y coordinate with our current location. Since Manhattan distance is simply the sum of absolute differences in coordinates, we can efficiently determine the nearest valid point by iterating through the given list.  

# Approach  
1. **Iterate Through Points:** We loop through all given points and check if they are valid (i.e., they share either the same x or y coordinate).  
2. **Calculate Manhattan Distance:** For each valid point, compute the Manhattan distance using the formula:  
   `distance = abs(x1 - x2) + abs(y1 - y2)` 
3. **Track the Closest Valid Point:** Maintain variables to store the smallest distance found and its corresponding index. If we find a closer valid point, update our stored values.  
4. **Return the Result:** After checking all points, return the index of the closest valid point, or -1 if none exist.  

# Complexity  
- **Time complexity:** O(n), since we iterate through the list of points once.  
- **Space complexity:** O(1), as we only use a few extra variables for tracking the minimum distance and index.  

# Code
```python3 []
class Solution:
    def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
        dist = float('inf')
        count = -1
        for i , p in enumerate(points):
            if p[0]==x or p[1]==y:
                man_dist = abs(p[0]-x)+abs(p[1]-y)
                if man_dist < dist:
                    count = i
                    dist = man_dist
        return count
```
['Python3']