<p>You are given two integers, <code>x</code> and <code>y</code>, which represent your current location on a Cartesian grid: <code>(x, y)</code>. You are also given an array <code>points</code> where each <code>points[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents that a point exists at <code>(a<sub>i</sub>, b<sub>i</sub>)</code>. A point is <strong>valid</strong> if it shares the same x-coordinate or the same y-coordinate as your location.</p>

<p>Return <em>the index <strong>(0-indexed)</strong> of the <strong>valid</strong> point with the smallest <strong>Manhattan distance</strong> from your current location</em>. If there are multiple, return <em>the valid point with the <strong>smallest</strong> index</em>. If there are no valid points, return <code>-1</code>.</p>

<p>The <strong>Manhattan distance</strong> between two points <code>(x<sub>1</sub>, y<sub>1</sub>)</code> and <code>(x<sub>2</sub>, y<sub>2</sub>)</code> is <code>abs(x<sub>1</sub> - x<sub>2</sub>) + abs(y<sub>1</sub> - y<sub>2</sub>)</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> x = 3, y = 4, points = [[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The answer is allowed to be on the same location as your current location.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> x = 3, y = 4, points = [[2,3]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There are no valid points.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= points.length &lt;= 10<sup>4</sup></code></li>
	<li><code>points[i].length == 2</code></li>
	<li><code>1 &lt;= x, y, a<sub>i</sub>, b<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def nearestValidPoint(self, target_x: int, target_y: int, points: List[List[int]]) -> int:
        # x_map = defaultdict(list)
        # y_map = defaultdict(list)
        # point_map = defaultdict(list)
        x_list = []
        y_list = []
        for i in range(len(points)):
            x, y = points[i]
            if x == target_x:
                x_list.append((y, i))
            if y == target_y:
                y_list.append((x, i))
        
        # for k in x_map.keys():
        #     x_map[k].sort()
        # for k in y_map.keys():
        #     y_map[k].sort()
        # min_distance = float(INF)
        min_distance = float('inf')
        min_index = -1
        for y_cood in x_list:
            if abs(y_cood[0] - target_y) < min_distance:
                min_index = y_cood[1]
                min_distance = abs(y_cood[0] - target_y)
            elif abs(y_cood[0] - target_y) == min_distance and y_cood[1] < min_index:
                min_index = y_cood[1]
                min_distance = abs(y_cood[0] - target_y)
        for x_cood in y_list:
            if abs(x_cood[0] - target_x) < min_distance:
                min_index = x_cood[1]
                min_distance = abs(x_cood[0] - target_x)
            elif abs(x_cood[0] - target_x) == min_distance and x_cood[1] < min_index:
                min_index = x_cood[1]
                min_distance = abs(x_cood[0] - target_x)
  
        # if x in x_map:
        #     # y_cood_list = x_map[target_x]
            
        #     for y_cood in y_cood_list:
        #         if abs(y_cood[0] - target_y) < min_distance:
        #             min_index = y_cood[1]
        #             min_distance = abs(y_cood[0] - target_y)
        #         elif abs(y_cood[0] - target_y) == min_distance and y_cood[1] < min_index:
        #             min_index = y_cood[1]
        #             min_distance = abs(y_cood[0] - target_y)
        # if y in y_map:
        #     x_cood_list = y_map[target_y]
        #     # print(x_cood_list)
        #     for x_cood in x_cood_list:
        #         if abs(x_cood[0] - target_x) < min_distance:
        #             min_index = x_cood[1]
        #             min_distance = abs(x_cood[0] - target_x)
        #             # print(min_index)
        #         elif abs(x_cood[0] - target_x) == min_distance and x_cood[1] < min_index:
        #             min_index = x_cood[1]
        return min_index    

        

        
567