question_slug
stringlengths 3
77
| title
stringlengths 1
183
| slug
stringlengths 12
45
| summary
stringlengths 1
160
β | author
stringlengths 2
30
| certification
stringclasses 2
values | created_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| updated_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| hit_count
int64 0
10.6M
| has_video
bool 2
classes | content
stringlengths 4
576k
| upvotes
int64 0
11.5k
| downvotes
int64 0
358
| tags
stringlengths 2
193
| comments
int64 0
2.56k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
find-nearest-point-that-has-the-same-x-or-y-coordinate | Simple for Beginners Java Approach | simple-for-beginners-java-approach-by-ja-xvcv | Q & A\n##### Q1: Why have you initialized index with -1 and not 0?\n##### \n##### A1: Make the code more general. e.g., If no point has the same x or y coordina | jainshubham766 | NORMAL | 2022-03-18T06:22:53.586989+00:00 | 2022-03-18T06:22:53.587034+00:00 | 4,060 | false | ##### Q & A\n##### Q1: Why have you initialized index with -1 and not 0?\n##### \n##### A1: Make the code more general. e.g., If no point has the same x or y coordinate, then we can still detect it by the return value. Otherwise, if the return value is 0, we would NOT know whether the point at index 0 is the solution or not.\n\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int min=Integer.MAX_VALUE, index=-1, i;\n \n for ( i=0;i<points.length;i++){\n if (x==points[i][0] || y==points[i][1]){\n int d = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);\n if (d<min){\n min=d;\n index=i;\n }\n }\n \n }\n // if ( min== Integer.MAX_VALUE) return -1; --> no longer needed as index is initialized as -1 in the declartion.\n return index;\n \n }\n}\n```\n\nPlease Upvote if it help :) (it boost my confidence!) | 48 | 0 | ['Java'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | β
O(N) Simple solution w/ explanation | C++ short & concise | on-simple-solution-w-explanation-c-short-fjwz | We can simply iterate over all the points in points array and if a point is valid (has the same x or y co-ordinate as our location), find its manhattan distance | archit91 | NORMAL | 2021-03-06T16:02:20.395418+00:00 | 2021-05-25T17:25:12.304874+00:00 | 4,174 | false | We can simply iterate over all the points in `points` array and if a point is valid (has the same `x` or `y` co-ordinate as our location), find its *manhattan distance* and remember its index if that is the minimum one. Lastly, just return the index.\n```\nint nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n\tint n = points.size(), mn = INT_MAX, ans = -1, manhattan;\n\tfor(int i = 0; i < n; i++)\n\t\tif(points[i][0] == x || points[i][1] == y){\n\t\t\tmanhattan = abs(x - points[i][0]) + abs(y - points[i][1]);\n\t\t\tif(manhattan < mn)\n\t\t\t\tmn = manhattan, ans = i; \n\t\t}\n\treturn ans;\n}\n```\n**Time Complexity** : **`O(N)`**, where `N` is the numbe of points in the `points` vector.\n**Space Complexity** : **`O(1)`**, since only constant space is used. | 38 | 4 | [] | 5 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [Python] Easy solution | python-easy-solution-by-arkumari2000-n9zi | \nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n minDist = math.inf\n ans = -1\n for i | arkumari2000 | NORMAL | 2021-05-25T04:24:57.626273+00:00 | 2021-05-25T04:24:57.626360+00:00 | 5,416 | false | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n minDist = math.inf\n ans = -1\n for i in range(len(points)):\n if points[i][0]==x or points[i][1]==y:\n manDist = abs(points[i][0]-x)+abs(points[i][1]-y)\n if manDist<minDist:\n ans = i\n minDist = manDist\n return ans\n \n``` | 31 | 0 | ['Python', 'Python3'] | 5 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | javascript 100% 100% simple | javascript-100-100-simple-by-soley-gt4f | \nvar nearestValidPoint = function(x, y, points) {\n let min = Infinity\n let idx = -1\n points.forEach(([a,b], i)=>{\n if(a===x || b===y){\n | soley | NORMAL | 2021-03-07T13:27:03.683820+00:00 | 2021-03-07T13:27:03.683862+00:00 | 1,758 | false | ```\nvar nearestValidPoint = function(x, y, points) {\n let min = Infinity\n let idx = -1\n points.forEach(([a,b], i)=>{\n if(a===x || b===y){\n const dist = Math.abs(x-a) + Math.abs(y-b)\n if(dist<min){\n idx = i\n min = dist\n }\n }\n })\n return idx\n};\n``` | 27 | 0 | ['JavaScript'] | 3 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [C++] Easy solution || 100% Time || 100% Space | c-easy-solution-100-time-100-space-by-_a-g9py | Simple solution in O(n) complexity.\n\nC++\n\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int pos | _A_Man | NORMAL | 2021-03-07T07:57:43.191303+00:00 | 2021-03-08T06:10:57.964098+00:00 | 2,508 | false | Simple solution in `O(n)` complexity.\n\n**C++**\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int pos = -1;\n int ans = INT_MAX;\n \n for(int i=0; i<points.size(); i++){\n if(points[i][0] == x or points[i][1] == y){\n int dist = abs(x-points[i][0]) + abs(y-points[i][1]);\n if(dist < ans){\n pos = i;\n ans = dist;\n }\n } \n }\n return pos;\n }\n};\n``` | 14 | 0 | ['C', 'C++'] | 2 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | β
Easy C++ O(N) Soln. [ FAANGπ± Interview Optimized code ] | easy-c-on-soln-faang-interview-optimized-0ebt | Find Nearest Point That Has the Same X or Y Coordinate\n1) Loop through the vector of points.\n2) If x or y coordinate matches, Calculate and record manhatten d | AdityaBhate | NORMAL | 2022-11-03T11:57:45.675383+00:00 | 2022-11-03T11:59:33.788549+00:00 | 941 | false | `Find Nearest Point That Has the Same X or Y Coordinate`\n1) Loop through the vector of points.\n2) If x or y coordinate matches, Calculate and record manhatten distance for every point.\n3) Compare every time if manhattan distance is lesser than the previous recorded least distance.\n4) Return the least distance point\'s index no. \n\n# **Pretty ez question ig ! Really good for beginners ! \uD83D\uDD25**\n\n```\nTC: O(n)\nSC: O(1)\n```\n\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int manhattan,d=INT_MAX,ans=-1;\n for(int i=0;i<points.size();i++){\n if(points[i][0]==x || points[i][1]==y){\n manhattan=abs(x - points[i][0]) + abs(y - points[i][1]);\n if(manhattan<d){\n d=manhattan;\n ans=i;\n }\n }\n }\n return ans;\n }\n};\n```\n# ***Please upvote if it helps \uD83D\uDE4F.*** | 8 | 0 | ['Array', 'C', 'Python', 'C++', 'Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python Beginners | 85% Fast | | python-beginners-85-fast-by-samarthbaron-bnf2 | Upvote if it helped. Thanks\n\n\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n min_dis= 10000 \n | SamarthBaronia | NORMAL | 2022-08-20T15:42:44.589257+00:00 | 2022-08-20T15:42:44.589292+00:00 | 1,214 | false | Upvote if it helped. Thanks\n\n```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n min_dis= 10000 \n ind = -1\n for i in range (len (points)):\n if points[i][0] == x or points [i][1] == y:\n mandist = abs(points[i][0] -x) + abs(points[i][1] - y)\n if mandist < min_dis :\n min_dis = mandist\n ind = i\n return ind\n``` | 8 | 1 | ['Python'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [C++] Linear Solution Explained, 100% Time, 100% Space | c-linear-solution-explained-100-time-100-qlir | Pleasant warm up - easy to understand and to execute.\n\nTo solve this problem, we will need 2 support variables first:\n res is where we will store the positio | Ajna2 | NORMAL | 2021-03-06T23:05:00.634342+00:00 | 2021-03-06T23:05:00.634375+00:00 | 1,262 | false | Pleasant warm up - easy to understand and to execute.\n\nTo solve this problem, we will need 2 support variables first:\n* `res` is where we will store the position of the first best fit we find, initially set to `-1` (which will then be returned when no match has been found);\n* `bestDist` will store the current closest distance across all the matches we found - initially set to `INT_MAX`.\n\nWe will then loop through all the elements in `points` and for each one we will:\n* assign the 2 values inside `points` as `cx` and `cy`;\n* check if `x == cx || y == cy` as per requirements and, in case:\n\t* compute `currDist` as the Manhattan distance between `{x, y}` and `{cx, cy}`;\n\t* check if `currDist < bestDist` and in case update both `bestDist` and `res` accordingly.\n\nOnce done, we can return `res` :)\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n // support variables\n int res = -1, bestDist = INT_MAX;\n // parsing points\n for (int i = 0, cx, cy, currDist, lmt = points.size(); i < lmt; i ++) {\n // assigning cx and cy\n cx = points[i][0], cy = points[i][1];\n // looking for a match\n if (x == cx || y == cy) {\n // computing currDist and in case updating bestDist and res\n currDist = abs(y - cy) + abs(x - cx);\n if (currDist < bestDist) {\n bestDist = currDist, res = i;\n }\n }\n }\n return res;\n }\n};\n``` | 8 | 0 | ['C', 'C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java | easy to understand | java-easy-to-understand-by-venkat089-yujy | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Venkat089 | NORMAL | 2022-12-27T06:38:55.615527+00:00 | 2022-12-27T06:38:55.615567+00:00 | 1,011 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int ans=-1;\n int temp=10001;\n for(int i=0;i<points.length;i++)\n {\n if(points[i][0]==x||points[i][1]==y)\n {\n int val=Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y);\n if(val<temp){\n temp=val;\n ans=i;\n }\n }\n }\n return ans;\n\n }\n}\n``` | 7 | 0 | ['Java'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java. Arrays. Find Nearest Point That Has the Same X or Y Coordinate | java-arrays-find-nearest-point-that-has-0vbu0 | \n\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int minManh = Integer.MAX_VALUE;\n int indMin = -1;\n | red_planet | NORMAL | 2023-05-08T09:27:17.919420+00:00 | 2023-05-08T09:27:17.919451+00:00 | 624 | false | \n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int minManh = Integer.MAX_VALUE;\n int indMin = -1;\n for (int i = 0; i < points.length; i++)\n {\n int tmpManh = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);\n if ((x == points[i][0] || y == points[i][1]) && (tmpManh < minManh))\n {\n minManh = tmpManh;\n indMin = i;\n }\n }\n return indMin;\n }\n}\n``` | 6 | 0 | ['Array', 'Java'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | python solution easy to understand O(n) , O(1) | python-solution-easy-to-understand-on-o1-r56c | Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n# Code\n\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[in | sintin1310 | NORMAL | 2022-11-25T09:10:59.153109+00:00 | 2022-11-25T09:10:59.153146+00:00 | 1,135 | false | # Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n# Code\n```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n\n minn=float(\'inf\')\n index=-1\n for i , v in enumerate(points):\n if v[0]==x or v[1]==y:\n man_dis=abs(x - v[0]) + abs(y - v[1])\n if(man_dis<minn) :\n minn=man_dis\n index=i\n \n return index\n``` | 6 | 0 | ['Python3'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | π Python3 simple naive approach | python3-simple-naive-approach-by-dark_wo-2xoo | \nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n result = None\n maximumDistance = float("inf | dark_wolf_jss | NORMAL | 2022-07-01T03:41:42.107439+00:00 | 2022-07-01T03:41:42.107468+00:00 | 1,079 | false | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n result = None\n maximumDistance = float("inf")\n \n for index,point in enumerate(points):\n a,b = point\n distance = abs(x-a) + abs(y-b)\n if distance < maximumDistance and (x == a or y == b):\n maximumDistance = distance\n result = index\n \n return result if result != None else -1\n``` | 6 | 0 | ['Python3'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | SUPER-UGLY Python one-liner for those who are into this stuff :D | super-ugly-python-one-liner-for-those-wh-ojmu | \nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n return min(map(lambda p:[abs(p[0] - x) + abs(p[1] - | qqzzqq | NORMAL | 2022-03-15T21:58:14.644709+00:00 | 2022-03-15T21:58:14.644763+00:00 | 564 | false | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n return min(map(lambda p:[abs(p[0] - x) + abs(p[1] - y), p[2]], list(filter(lambda p:p[0] == x or p[1] == y, map(lambda l:l[1] + [l[0]], enumerate(points)))) or [[0,0,-1]]))[1]\n``` | 6 | 0 | ['Python'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java | Simple | Explained | java-simple-explained-by-prashant404-il45 | T/S: O(n)/O(1), where n = size(points)\n\npublic int nearestValidPoint(int x, int y, int[][] points) {\n\tvar nearest = -1;\n\n\tfor (int i = 0, minDistance = I | prashant404 | NORMAL | 2022-01-31T02:17:00.304364+00:00 | 2022-01-31T02:17:00.304408+00:00 | 792 | false | **T/S:** O(n)/O(1), where n = size(points)\n```\npublic int nearestValidPoint(int x, int y, int[][] points) {\n\tvar nearest = -1;\n\n\tfor (int i = 0, minDistance = Integer.MAX_VALUE; i < points.length; i++)\n\t\tif (x == points[i][0] || y == points[i][1]) { // check valid point\n\t\t\tvar distance = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]); // find Manhattan distance\n\t\t\t\n\t\t\tif (minDistance > distance) { \n\t\t\t\tminDistance = distance; // maintain the nearest valid point\n\t\t\t\tnearest = i;\n\t\t\t}\n\t\t}\n\treturn nearest;\n}\n```\n***Please upvote if this helps*** | 6 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java || Easy to Understand || 1ms || beats 100% || O(points.length) | java-easy-to-understand-1ms-beats-100-op-icjf | \n // O(points.length) O(1)\n\tpublic int nearestValidPoint(int x, int y, int[][] points) {\n\n\t\tint min = Integer.MAX_VALUE, ans = -1, idx = -1;\n\t\tfor | LegendaryCoder | NORMAL | 2021-03-08T10:45:45.045591+00:00 | 2021-03-08T10:45:45.045628+00:00 | 1,379 | false | \n // O(points.length) O(1)\n\tpublic int nearestValidPoint(int x, int y, int[][] points) {\n\n\t\tint min = Integer.MAX_VALUE, ans = -1, idx = -1;\n\t\tfor (int[] point : points) {\n\t\t\tidx++;\n\t\t\tif (point[0] == x || point[1] == y) {\n\t\t\t\tint dist = distance(x, y, point[0], point[1]);\n\t\t\t\tif (dist < min) {\n\t\t\t\t\tmin = dist;\n\t\t\t\t\tans = idx;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn ans;\n\t}\n\n\t// O(1)\n\tpublic int distance(int x1, int y1, int x2, int y2) {\n\t\treturn Math.abs(x1 - x2) + Math.abs(y1 - y2);\n\t} | 6 | 0 | [] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python easy solution O(n) | python-easy-solution-on-by-it_bilim-uk7j | \ndef nearestValidPoint(self, x, y, p):\n ind = -1\n mn = 100000000000000\n for i in range(len(p)):\n if x == p[i][0] or y == p[ | it_bilim | NORMAL | 2021-03-06T16:57:32.957972+00:00 | 2021-03-06T16:58:28.266611+00:00 | 951 | false | ```\ndef nearestValidPoint(self, x, y, p):\n ind = -1\n mn = 100000000000000\n for i in range(len(p)):\n if x == p[i][0] or y == p[i][1]:\n t = abs(x - p[i][0]) + abs(y - p[i][1])\n if t < mn:\n mn, ind = t, i\n return ind\n``` | 6 | 3 | ['Python'] | 2 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [JAVA]β | Easy and Explained Solution β€ | Speed Beats 100% βοΈ | java-easy-and-explained-solution-speed-b-8s0e | Approach\n Describe your approach to solving the problem. \nThe solution involves a simple iteration of the array. \n\nAt each iteration we have to check if the | user0970Ja | NORMAL | 2023-03-06T19:30:44.451575+00:00 | 2023-03-06T19:30:44.451624+00:00 | 1,171 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nThe solution involves a simple **iteration of the array**. \n\n*At each iteration* we have to check if **the point is valid**, in this case we have to check if the distance of this point is shorter than the distance that we found in previous iterations.\n\n**Note:** The initialization of sIndex and sDistance is higher than the *maximum value allowed by the input conditions*.\n \n# Complexity\n- **Time complexity:** $O(n)$\n- **Speed Beats:** 100%\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- **Space complexity:** $O(1)$\n- **Memory Beats:** 78.3%\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n \n int sIndex = 100000;\n int sDistance = 100000;\n\n for (int i = 0; i < points.length; i++) {\n \n // Check if is a valid point\n if (points[i][0] == x || points[i][1] == y) {\n\n // Check if it is better than the previous best\n int distance = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);\n if (sDistance > distance) {\n sDistance = distance;\n sIndex = i;\n }\n }\n }\n\n // Check if we have a valid point to return\n if (sIndex == 100000)\n return -1;\n return sIndex;\n }\n}\n``` | 5 | 0 | ['Array', 'Java'] | 2 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | βπ― simple solution with explanation | simple-solution-with-explanation-by-atha-aiqz | \nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) \n {\n /* \n\t\tHere we are just iterating over the | atharva_kulkarni1 | NORMAL | 2022-11-28T18:01:02.985998+00:00 | 2022-11-28T18:05:43.750330+00:00 | 267 | false | ```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) \n {\n /* \n\t\tHere we are just iterating over the given array and if we get any \n\t\tvalid point (i.e. it shares either x-coordinate OR y-coordinate with\n\t\tour obtained points) and if the currDist is more than ManhattanDist\n\t\tthen we update it and over this whole process we maintain a variable \n\t\tfor storing the smallest index.\n \n TC -> O(N) , N = number of points in points array\n SC -> O(1) \n\t\t\n\t\t*/\n \n \n int res = -1;\n int n = points.size();\n int dist = INT_MAX;\n int ManhattanDist;\n \n for(int i=0; i<n; i++)\n {\n if(points[i][0] == x || points[i][1] == y)\n {\n ManhattanDist = abs(x-points[i][0]) + abs(y-points[i][1]);\n \n if(ManhattanDist < dist)\n {\n dist = ManhattanDist;\n res = i;\n }\n }\n }\n \n \n return res;\n }\n};\n``` | 5 | 0 | ['C', 'C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easiest and Fastest Solution | easiest-and-fastest-solution-by-parulsah-i8a7 | Please Upvote (^_^)\n\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] point) {\n int ans=Integer.MAX_VALUE;\n int var=-1 | parulsahni621 | NORMAL | 2022-09-26T16:32:11.074512+00:00 | 2022-09-26T16:32:11.074554+00:00 | 755 | false | Please Upvote (^_^)\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] point) {\n int ans=Integer.MAX_VALUE;\n int var=-1;\n for(int i=0;i<point.length;i++)\n {\n \n if(point[i][0]==x || point[i][1]==y)\n {\n int m=Math.abs(x-point[i][0])+Math.abs(y-point[i][1]);\n if(m<ans)\n {\n ans=m;\n var=i;\n }\n }\n }\n return var;\n }\n}\n``` | 5 | 0 | [] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | JavaScript Solution Using a Map Object | javascript-solution-using-a-map-object-b-okzl | Found this solution helpful? Consider showing support by upvoting this post. If there are any questions, kindly leave a comment below. Thank you and happy hacki | sronin | NORMAL | 2022-06-22T02:34:20.674072+00:00 | 2022-07-23T23:10:29.701725+00:00 | 485 | false | Found this solution helpful? Consider showing support by upvoting this post. If there are any questions, kindly leave a comment below. Thank you and happy hacking!\n```\nvar nearestValidPoint = function (x, y, points) {\n let manhattanIndices = new Map()\n let currentMin = Infinity\n\n points.forEach((point, i) => {\n if (point[0] === x || point[1] === y) {\n manhattanIndices.set(i, Math.abs(x - point[0]) + Math.abs(y - point[1]))\n currentMin = Math.min(currentMin, Math.abs(x - point[0]) + Math.abs(y - point[1]))\n }\n })\n\n for(let [index, distance] of manhattanIndices){\n if(distance === currentMin){\n return index\n }\n }\n\n return -1\n};\n``` | 5 | 0 | ['JavaScript'] | 3 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python - Clean and Simple! | python-clean-and-simple-by-domthedevelop-6kmy | Solution:\n\nclass Solution:\n def nearestValidPoint(self, x1, y1, points):\n minIdx, minDist = -1, inf\n for i,point in enumerate(points):\n | domthedeveloper | NORMAL | 2022-05-11T08:29:44.663358+00:00 | 2022-05-11T08:29:44.663403+00:00 | 1,212 | false | **Solution**:\n```\nclass Solution:\n def nearestValidPoint(self, x1, y1, points):\n minIdx, minDist = -1, inf\n for i,point in enumerate(points):\n x2, y2 = point\n if x1 == x2 or y1 == y2:\n dist = abs(x1-x2) + abs(y1-y2)\n if dist < minDist:\n minIdx = i\n minDist = min(dist,minDist)\n return minIdx\n``` | 5 | 0 | ['Array', 'Python', 'Python3'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [Ruby] O(n) | ruby-on-by-mistersky-y2fr | \n# @param {Integer} x\n# @param {Integer} y\n# @param {Integer[][]} points\n# @return {Integer}\ndef nearest_valid_point(x, y, points)\n distances = []\n \n | mistersky | NORMAL | 2021-03-07T08:30:41.179258+00:00 | 2021-03-07T08:30:41.179300+00:00 | 121 | false | ```\n# @param {Integer} x\n# @param {Integer} y\n# @param {Integer[][]} points\n# @return {Integer}\ndef nearest_valid_point(x, y, points)\n distances = []\n \n points.each_with_index do |point, index|\n a, b = point\n distances << [(x - a).abs + (y - b).abs, index] if a == x || b == y\n end\n \n return -1 if distances.empty?\n \n distances.sort.first.last\nend\n``` | 5 | 0 | ['Ruby'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | C++ Solution | c-solution-by-pranto1209-w4bl | Code\n\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int ans = -1, mdis = INT_MAX;\n for(in | pranto1209 | NORMAL | 2022-12-24T05:46:54.989500+00:00 | 2023-03-24T10:57:39.498304+00:00 | 991 | false | # Code\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int ans = -1, mdis = INT_MAX;\n for(int i=0; i<points.size(); i++) {\n if(points[i][0] == x or points[i][1] == y) {\n int dis = abs(points[i][0] - x) + abs(points[i][1] - y);\n if(dis < mdis) mdis = dis, ans = i;\n }\n }\n return ans;\n }\n};\n``` | 4 | 0 | ['C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | βοΈ explained solution | beats 90% in time | 95% in space | C++ | explained-solution-beats-90-in-time-95-i-xsra | We first check whether there are common coordinates between the given (x,y) & elements of the given array. If there is we compare it with the previous Manhattan | coding_menance | NORMAL | 2022-10-26T09:29:42.547552+00:00 | 2022-10-26T09:30:54.125106+00:00 | 1,701 | false | We first check whether there are common coordinates between the given (x,y) & elements of the given array. If there is we compare it with the previous **Manhattan distance** & if it is less we update the index to be returned to the current one [remember we are asked for the index of the point with smallest Manhattan distance and not the distance itself].\n\nI\'ve used ternary operators here, which are bascially a concise form of if else statements, it reduces lines of code and makes your work look clean, I recommend you learning it.\n\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int M=INT_MAX, soln=-1;\n for (int i=0; i<points.size(); i++) {\n \n // let\'s see if there are any common coordinates or not\n if (x==points[i][0] || y==points[i][1]) {\n\n // let\'s store the Manhattan distance to variable t\n int t= abs(y-points[i][1]) + abs(x-points[i][0]);\n\n // M stores the previous Manhattan distance value. If the current distance is less we update the index\n // even if the distance is equal, we don\'t update the index as we\'re asked for the lowest index\n soln=M>t ? i:soln;\n\n // we also update the Manhattan distance, for future calculations\n M = M<t ? M:t;\n }\n }\n\n // we return the index of the point with least Manhattan distance\n return soln;\n }\n};\n```\n\n*Hey! If this post clearified your understanding on how to approach the question, please upvote this solution \uD83D\uDCA1* | 4 | 0 | ['Array', 'C++'] | 2 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | C++ | c-by-easy0peasy1-oj0m | \nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int n = points.size();\n int temp = INT_MAX; | easy0peasy1 | NORMAL | 2022-07-17T13:52:27.389711+00:00 | 2022-07-17T13:52:27.389752+00:00 | 344 | false | ```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int n = points.size();\n int temp = INT_MAX;\n int distance;\n int ans = INT_MIN;\n for(int i = 0; i < n; i++) {\n if(points[i][0] == x || points[i][1] == y) {\n distance = (abs(x-points[i][0]) + abs(y-points[i][1]));\n if (temp > distance) {\n temp = distance;\n ans = i;\n } \n }\n }\n return ans == INT_MIN ? -1 : ans;\n }\n};\n``` | 4 | 0 | ['C'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | easily understandable code | easily-understandable-code-by-saswata_ra-eias | \nclass Solution(object):\n def nearestValidPoint(self, x, y, points):\n """\n :type x: int\n :type y: int\n :type points: List[L | Saswata_Rakshit | NORMAL | 2022-03-12T17:19:34.147319+00:00 | 2022-03-12T17:19:34.147364+00:00 | 296 | false | ```\nclass Solution(object):\n def nearestValidPoint(self, x, y, points):\n """\n :type x: int\n :type y: int\n :type points: List[List[int]]\n :rtype: int\n """\n \n \n mdist=float(\'inf\')#assigning a large value to the manhattan distance\n index=-1#starting with index -1 because if we are not able to find any point then we can return -1 as mentioned in the question\n #after that we are going through each point\n for i in range(len(points)):\n if points[i][0] ==x or points[i][1]==y:#checking the condition to satisfy the manhattan conditions\n num=abs(points[i][0]-x)+abs(points[i][1]-y)#calculating the manhattan distance\n if num<mdist:\n mdist=num#if we are able to find any distance then updating it\n index=i#we are also storing its index also as we have to return it \n return index\n \n``` | 4 | 0 | ['Python'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [python] - easy to understand | python-easy-to-understand-by-sergimia-buyk | Iterating through the all points with enumerate(points) that returns index (i) and value (point)\nIf the current candidate (point) don\'t have either same X ax | SergiMia | NORMAL | 2021-03-06T16:05:02.238658+00:00 | 2021-03-06T16:32:19.027336+00:00 | 269 | false | Iterating through the all points with `enumerate(points)` that returns index (i) and value (point)\nIf the current candidate (point) don\'t have either same X axis or Y axis - skip it and go to the next one. Continue inside the loop only if current point is "valid".\nCalculate smallest distance (dist) by comparing to the maximum possible (20,000 due to constraints of the task)\nAs soon as we see better then current best - update the best (dist) and remember its index (i).\nIf we see as good as but **not** better abs(point[0] - x) + abs(point[1] - y) **<** dist - ignore\n\nReturn index of first best\n```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n dist = 20_000\n ans = -1\n for i, point in enumerate(points):\n if point[0] == x or point[1] == y:\n if abs(point[0] - x) + abs(point[1] - y) < dist:\n dist = abs(point[0] - x) + abs(point[1] - y)\n ans = i\n return ans\n``` | 4 | 2 | [] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python3 Easiest and Most understandable solution | python3-easiest-and-most-understandable-qyy3o | The solution has three parts, \n1. Checking the validity of the given points.\n2. Finding the smallest Manhattan distance \n3. returning the index of the smalle | nehanayak | NORMAL | 2023-01-10T18:03:51.562154+00:00 | 2023-01-10T18:03:51.562196+00:00 | 2,028 | false | The solution has three parts, \n1. Checking the validity of the given points.\n2. Finding the smallest Manhattan distance \n3. returning the index of the smallest Manhattan distance \n\nWe check if there are no valid points and return -1\n\n# Code\n```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n valid=[]\n for i in points:\n if i[0]==x or i[1]==y:\n valid.append(i)\n dist=[]\n if len(valid) == 0:\n return -1\n else :\n for i in valid:\n dist.append(abs(x - i[0]) + abs(y - i[1]))\n if valid[dist.index(min(dist))] in valid:\n return points.index(valid[dist.index(min(dist))])\n\n\n\n\n \n\n``` | 3 | 0 | ['Python3'] | 2 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | c++ | easy | short | c-easy-short-by-venomhighs7-q5m3 | \n# Code\n\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int ind = -1;\n int dist = INT_MAX; | venomhighs7 | NORMAL | 2022-10-12T03:29:34.349488+00:00 | 2022-10-12T03:29:34.349528+00:00 | 770 | false | \n# Code\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int ind = -1;\n int dist = INT_MAX; \n for(int i =0; i<points.size(); i++){\n if(points[i][0] == x || points[i][1] == y){\n int temp = abs(x-points[i][0])+abs(y-points[i][1]);\n if(dist > temp){\n dist = temp;\n ind = i;\n }\n \n }\n }\n return ind;\n }\n};\n``` | 3 | 0 | ['C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | C++ || Brute Force approach || Easy to Understand | c-brute-force-approach-easy-to-understan-ppk8 | \nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int mi=INT_MAX,res=-1;\n for(int i=0;i<point | srikrishna0874 | NORMAL | 2022-09-19T12:54:55.737234+00:00 | 2022-09-19T12:54:55.737274+00:00 | 51 | false | ```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int mi=INT_MAX,res=-1;\n for(int i=0;i<points.size();i++)\n {\n int x1=points[i][0],y1=points[i][1];\n if(x==x1||y==y1) \n {\n int dis=abs(x1-x)+abs(y1-y);\n if(mi>dis) \n {\n mi=dis;\n res=i;\n }\n }\n }\n return res;\n }\n};\n```\nIf you like it, please upvote.. | 3 | 0 | ['C'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [JS] Easy greedy O(n) solution | js-easy-greedy-on-solution-by-hugotannus-nxvx | \nvar nearestValidPoint = function(x, y, points) {\n let distance = 99999, validIndex = -1;\n \n for(let i in points) {\n const [a, b] = points[ | hugotannus | NORMAL | 2022-08-17T00:16:11.466744+00:00 | 2022-08-17T00:17:43.042471+00:00 | 257 | false | ```\nvar nearestValidPoint = function(x, y, points) {\n let distance = 99999, validIndex = -1;\n \n for(let i in points) {\n const [a, b] = points[i];\n if(a != x && b != y) continue;\n \n let manhattan = Math.abs(x-a) || Math.abs(y-b);\n \n // It is not necessary to continue if found (x,y) coordinate\n if(manhattan === 0) return i;\n if(manhattan < distance) [distance, validIndex] = [manhattan, i]\n }\n \n return validIndex;\n};\n``` | 3 | 0 | ['Greedy', 'JavaScript'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [JAVA] simple and easy solution | java-simple-and-easy-solution-by-juganta-i8cm | \nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n var nearest = - 1;\n\n\tfor (int i = 0, minDistance = Integer.MAX_V | Jugantar2020 | NORMAL | 2022-08-14T06:30:50.523483+00:00 | 2022-08-14T06:30:50.523523+00:00 | 218 | false | ```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n var nearest = - 1;\n\n\tfor (int i = 0, minDistance = Integer.MAX_VALUE; i < points.length; i ++)\n\t\tif (x == points[i][0] || y == points[i][1]) { // check valid point\n\t\t\tvar distance = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]); // find Manhattan distance\n\t\t\t\n\t\t\tif (minDistance > distance) { \n\t\t\t\tminDistance = distance; // maintain the nearest valid point\n\t\t\t\tnearest = i;\n\t\t\t}\n\t\t}\n\treturn nearest; \n }\n}\n```\n# PLEASE UPVOTE IF IT WAS HELPFULL | 3 | 0 | ['Array', 'Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy JavaScript Solution | easy-javascript-solution-by-azensky-o65k | \nvar nearestValidPoint = function(x, y, points) {\n let minDist = Infinity;\n let res;\n \n for (let i = 0; i < points.length; i++) {\n let | AZensky | NORMAL | 2022-08-04T21:04:07.137690+00:00 | 2022-08-04T21:04:07.137724+00:00 | 389 | false | ```\nvar nearestValidPoint = function(x, y, points) {\n let minDist = Infinity;\n let res;\n \n for (let i = 0; i < points.length; i++) {\n let [currX, currY] = points[i];\n \n if (currX === x || currY === y) {\n let dist = Math.abs(x - currX) + Math.abs(y - currY);\n if (dist < minDist) {\n minDist = dist;\n res = i;\n }\n }\n }\n \n if (res === 0) return 0;\n \n return res ? res : -1\n};\n``` | 3 | 0 | ['JavaScript'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | SSS: Simple Swift Solution β
β
| sss-simple-swift-solution-by-mishe1e-h8ap | \nfunc nearestValidPoint(_ x: Int, _ y: Int, _ points: [[Int]]) -> Int {\n\tvar (result, dMin) = (-1, Int.max)\n\n\tfor (i, point) in points.enumerated() {\n\t\ | mishe1e | NORMAL | 2022-04-28T06:20:24.886110+00:00 | 2022-12-09T04:54:24.652267+00:00 | 139 | false | ```\nfunc nearestValidPoint(_ x: Int, _ y: Int, _ points: [[Int]]) -> Int {\n\tvar (result, dMin) = (-1, Int.max)\n\n\tfor (i, point) in points.enumerated() {\n\t\tlet x1 = point[0], y1 = point[1]\n\t\tguard x == x1 || y == y1 else { continue }\n\t\tlet d = abs(x - x1) + abs( y - y1)\n\t\tif d < dMin {\n\t\t\tdMin = d\n\t\t\tresult = i\n\t\t}\n\t}\n\n\treturn result\n}\n```\n\nor\n\n```\nfunc nearestValidPoint(_ x: Int, _ y: Int, _ points: [[Int]]) -> Int {\n let notFoundIndexValue = -1\n var (result, mdMin) = (notFoundIndexValue, 0)\n\n for (i, point) in points.enumerated() {\n let (x1, y1) = (point[0], point[1])\n guard x == x1 || y == y1 else { continue }\n let md = abs(x - x1) + abs(y - y1)\n guard result != notFoundIndexValue else {\n (result, mdMin) = (i, md)\n continue\n }\n if md < mdMin { (result, mdMin) = (i, md) }\n }\n\n return result\n}\n``` | 3 | 0 | ['Swift'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Simple python solution | simple-python-solution-by-arizala13-zouf | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n # answer array to insrt the index\n answe | arizala13 | NORMAL | 2022-04-23T19:30:01.171836+00:00 | 2022-04-23T19:30:01.171878+00:00 | 281 | false | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n # answer array to insrt the index\n answer = -1\n # starting point for distance to compare\n man_distance = float("inf")\n \n # allows east insert to answer arrow and increment\n count = 0 \n \n # loop through point\n for point in points:\n if point[0] == x or point[1] == y:\n # compare \n current_man_distance = abs(point[0] - x) + abs(point[1] - y)\n if current_man_distance < man_distance:\n # update the man_distance\n man_distance = current_man_distance\n answer = count\n \n # increment count\n count = count + 1\n \n return answer\n\t\t | 3 | 0 | ['Python'] | 2 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java || faster than 100% || Simple solution | java-faster-than-100-simple-solution-by-nn8rx | \nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int min = Integer.MAX_VALUE;\n int index = -1;\n for | funingteng | NORMAL | 2022-03-30T14:36:26.834799+00:00 | 2022-03-30T14:36:26.834824+00:00 | 305 | false | ```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int min = Integer.MAX_VALUE;\n int index = -1;\n for (int i = 0; i < points.length; i++) {\n int[] point = points[i];\n if (point[0] == x) {\n int d = Math.abs(y - point[1]);\n if (d < min) {\n index = i;\n min = d;\n }\n } else if (point[1] == y) {\n int d = Math.abs(x - point[0]);\n if (d < min) {\n index = i;\n min = d;\n }\n }\n }\n return index;\n }\n}\n``` | 3 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | β
[Python] Simple Solution | python-simple-solution-by-iamprateek-vluw | \ndef nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n ans = -1\n n = len(points)\n mini = float(inf)\n \n | iamprateek | NORMAL | 2022-03-05T18:06:00.136265+00:00 | 2022-03-05T18:06:00.136306+00:00 | 236 | false | ```\ndef nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n ans = -1\n n = len(points)\n mini = float(inf)\n \n for i in range(n):\n if points[i][0]==x or points[i][1]==y:\n dis = abs(points[i][0]-x) + abs(points[i][1]-y)\n \n if dis<mini:\n mini = dis\n ans = i\n \n return ans\n``` | 3 | 0 | ['Python3'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python clean solution | python-clean-solution-by-shimkoaa-kun2 | \nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n def compute_dist(x1, y1, x2, y2):\n retu | shimkoaa | NORMAL | 2022-03-04T07:37:49.657956+00:00 | 2022-03-04T07:37:49.658003+00:00 | 180 | false | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n def compute_dist(x1, y1, x2, y2):\n return abs(x1-x2) + abs(y1-y2)\n \n min_idx, min_dist = -1, float(\'inf\')\n \n for i, (x_hat, y_hat) in enumerate(points):\n if x_hat == x or y_hat == y:\n curr_dist = compute_dist(x, y, x_hat, y_hat)\n if curr_dist < min_dist:\n min_idx = i\n min_dist = curr_dist\n return min_idx\n``` | 3 | 0 | [] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy to understand JavaScript solution (reduce) | easy-to-understand-javascript-solution-r-8htm | \tvar nearestValidPoint = function(x, y, points) {\n\t\tconst valid = points.reduce((acc, [pointX, pointY], index) => {\n\t\t\tif (x === pointX || y === pointY) | tzuyi0817 | NORMAL | 2021-08-21T05:52:40.811648+00:00 | 2021-08-21T05:52:40.811680+00:00 | 211 | false | \tvar nearestValidPoint = function(x, y, points) {\n\t\tconst valid = points.reduce((acc, [pointX, pointY], index) => {\n\t\t\tif (x === pointX || y === pointY) {\n\t\t\t\tconst distance = Math.abs(x - pointX) + Math.abs(y - pointY);\n\n\t\t\t\tdistance < acc.distance && (acc = { distance, index });\n\t\t\t}\n\t\t\treturn acc;\n\t\t}, { distance: Infinity, index: -1 });\n\n\t\treturn valid.index;\n\t}; | 3 | 0 | ['JavaScript'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Elegant Python, C++ | elegant-python-c-by-aragorn-15wj | Python\n\nclass Solution:\n def nearestValidPoint(self, x, y, points):\n best = 100000\n where = -1\n \n for i,ab in enumerate(p | aragorn_ | NORMAL | 2021-03-14T22:02:40.910509+00:00 | 2021-06-02T15:48:33.921713+00:00 | 263 | false | **Python**\n```\nclass Solution:\n def nearestValidPoint(self, x, y, points):\n best = 100000\n where = -1\n \n for i,ab in enumerate(points):\n a,b = ab\n if a==x or b==y:\n d = abs(x-a) + abs(y-b)\n if d<best:\n best = d\n where = i\n return where\n```\n\n**C++**\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n \n int best = 100000;\n int where = -1;\n size_t L = points.size();\n int a,b,d;\n\t\t\n for (size_t i = 0; i<L; i++){\n a = points[i][0];\n b = points[i][1];\n if (a==x | b==y){\n d = abs(x-a) + abs(y-b);\n if (d<best){\n best = d;\n where = i;\n }\n }\n }\n return where;\n }\n};\n``` | 3 | 1 | ['C', 'Python', 'Python3'] | 2 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Check all points - O(n) time and O(1) space JavaScript solution | check-all-points-on-time-and-o1-space-ja-tuk1 | This is staraightforward problem, example implementation in JavaScript:\n\n\nvar nearestValidPoint = function(x, y, points) {\n // keep track of both the sho | user4125zi | NORMAL | 2021-03-06T16:13:15.227726+00:00 | 2021-03-06T16:13:15.227775+00:00 | 256 | false | This is staraightforward problem, example implementation in JavaScript:\n\n```\nvar nearestValidPoint = function(x, y, points) {\n // keep track of both the shortest distance and its index\n let ans = -1, ansDist = Number.MAX_SAFE_INTEGER;\n \n for (let i = 0; i < points.length; i++) {\n const [px, py] = points[i];\n if (px === x || py === y) {\n const dist = Math.abs(x - px) + Math.abs(y - py);\n if (dist < ansDist) {\n ansDist = dist;\n ans = i;\n }\n }\n }\n \n return ans;\n};\n```\n | 3 | 0 | ['JavaScript'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | C++ | very easy | c-very-easy-by-sonusharmahbllhb-w5wb | \nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int dist=INT_MAX;int ans=-1;\n for(int i=0;i | sonusharmahbllhb | NORMAL | 2021-03-06T16:02:54.238435+00:00 | 2021-03-06T16:02:54.238462+00:00 | 309 | false | ```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int dist=INT_MAX;int ans=-1;\n for(int i=0;i<points.size();i++)\n {\n if(points[i][0]==x or points[i][1]==y)\n {\n int mh=abs(x-points[i][0])+abs(points[i][1]-y);\n if(mh<dist)\n {\n dist=mh;\n ans=i;\n }\n }\n \n }\n return ans;\n }\n};\n``` | 3 | 2 | ['C', 'C++'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy Solutionπ₯π₯O(N) Solution ; Detailed ExplanationβοΈβοΈ | easy-solutionon-solution-detailed-explan-1kao | Intuition\n Describe your first thoughts on how to solve this problem. \nHere, we simply need to check if the point is valid with the minimum Manhattan distance | anshika_coder467 | NORMAL | 2024-06-20T08:51:25.372951+00:00 | 2024-06-20T08:51:25.372991+00:00 | 335 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHere, we simply need to check if the point is valid with the minimum **Manhattan distance** and return the index of it else return -1.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere the current location is given by x and y i.e. (x, y) coordinate.\n- ***The first FOR Loop***\nFirstly, we need to find the minimum Manhattan distance possible for the valid points\nThe valid point can be checked by the **if condition** for x and y to the corresponding x and y coordinates of the point.\nThen we will find the Manhattan Distance of the point from the current location and save this distance in **min** variable. This distance is updated accordingly with the minimum distance possible.\n- ***The second FOR Loop***\nNow, we will check which valid points have that minimum Manhattan Distance and return its index.\n\n- If there is no such valid point then return -1.\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int min = INT_MAX;\n for(int i = 0; i < points.size(); i++) {\n if( (x == points[i][0]) || (y == points[i][1]) ) {\n int d = abs(points[i][0] - x) + abs(points[i][1] - y);\n if(d < min) min = d; \n }\n }\n\n for(int i = 0; i < points.size(); i++) {\n if( (x == points[i][0]) || (y == points[i][1]) ) {\n int d1 = abs(points[i][0] - x) + abs(points[i][1] - y); \n if(d1 == min) return i;\n }\n }\n return -1;\n }\n};\n``` | 2 | 0 | ['C++'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | π¦ | 100% beaten golang solution | self-explanatory | 100-beaten-golang-solution-self-explanat-92s8 | Code\n\nfunc nearestValidPoint(x int, y int, points [][]int) int {\n mn := 10000\n ans := -1\n for ind, point := range points {\n if point[0] == | makarkananov | NORMAL | 2024-01-08T22:58:37.244024+00:00 | 2024-01-08T22:58:37.244073+00:00 | 40 | false | # Code\n```\nfunc nearestValidPoint(x int, y int, points [][]int) int {\n mn := 10000\n ans := -1\n for ind, point := range points {\n if point[0] == x || point[1] == y{\n if mn >int(math.Abs(float64(x - point[0])) + math.Abs(float64(y - point[1]))) {\n mn = int(math.Abs(float64(x - point[0])) + math.Abs(float64(y - point[1])))\n ans = ind\n }\n }\n }\n return ans\n}\n``` | 2 | 0 | ['Go'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Find the Nearest Valid Point to a Given Coordinate | find-the-nearest-valid-point-to-a-given-xdl9z | Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to find the index of the point in the given array that is closest to the given | RaimberdiyevB | NORMAL | 2023-04-29T15:41:15.766092+00:00 | 2023-04-29T15:41:15.766147+00:00 | 170 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to find the index of the point in the given array that is closest to the given (x, y) coordinate and has either the same x or y coordinate as the given coordinate.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe first create an ArrayList to store all the points that have either the same x or y coordinate as the given coordinate. We then iterate through this ArrayList and calculate the distance between each point and the given coordinate. We keep track of the minimum distance found so far and the corresponding point. Finally, we iterate through the input points array to find the index of the minimum distance point and return it.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n), where n is the number of points in the input array.\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(k), where k is the number of valid points \n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int res = -1;\n ArrayList<int[]> arr = new ArrayList<>();\n for (int i = 0; i < points.length; i++) {\n int xPoint = points[i][0];\n int yPoint = points[i][1];\n if(xPoint == x || yPoint == y){\n arr.add(points[i]);\n System.out.println("Point added to arr : " + Arrays.toString(points[i]));\n }\n }\n System.out.println("List of valid points : ");\n arr.forEach(ints -> {\n System.out.print(Arrays.toString(ints) + " ");\n });\n System.out.println();\n int min = Integer.MAX_VALUE;\n int[] minPoint = new int[]{0,0};\n if(!arr.isEmpty()) {\n for (int i = 0; i < arr.size(); i++) {\n int current = Math.abs(x - arr.get(i)[0]) + Math.abs(y - arr.get(i)[1]);\n System.out.println("Current distance : " + current);\n if (current < min) {\n min = current;\n minPoint = new int[]{arr.get(i)[0], arr.get(i)[1]};\n }\n System.out.println("Min distance : " + min);\n System.out.println("Min point : " + Arrays.toString(minPoint));\n }\n\n for (int i = 0; i < points.length; i++) {\n if (Arrays.equals(points[i], minPoint)) {\n res = i;\n System.out.println("Res value added " + i);\n return res;\n }\n }\n }\n return res;\n }\n}\n``` | 2 | 0 | ['Array', 'Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | β
Best and Easy solution 100% β
| best-and-easy-solution-100-by-amitmungar-ynii | \n\n# Code\n\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] mat) {\n int min =10001;\n int pos=-1;\n\n for(int i | amitmungare27 | NORMAL | 2023-04-01T09:43:55.017769+00:00 | 2023-04-01T09:43:55.017811+00:00 | 1,170 | false | \n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] mat) {\n int min =10001;\n int pos=-1;\n\n for(int i=0; i<mat.length; i++){\n if(mat[i][0]==x || mat[i][1]==y){\n int val = Math.abs(mat[i][0]-x)+Math.abs(mat[i][1]-y);\n if(val<min){\n min=val;\n pos=i;\n }\n }\n }\n return pos;\n }\n}\n``` | 2 | 0 | ['Array', 'Java'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | β
π₯ One line Python || β‘very easy solution | one-line-python-very-easy-solution-by-ma-11sn | \n\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n return min([(abs(p[0]-x)+abs(p[1]-y), i) for i, p | maary_ | NORMAL | 2023-03-16T17:33:03.867113+00:00 | 2023-03-16T17:33:03.867162+00:00 | 97 | false | \n```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n return min([(abs(p[0]-x)+abs(p[1]-y), i) for i, p in enumerate(points) if p[0]==x or p[1]==y], default=(-1, -1))[1]\n\n``` | 2 | 0 | ['Python', 'Python3', 'Python ML'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | π₯ C++ || Beats 99.60% || O(n) time, O(1) space || Easy to understand π₯ | c-beats-9960-on-time-o1-space-easy-to-un-a0ih | Intuition\n\n\n# Approach\nWe check for each point the coincidence of at least one of the coordinates. If there is a match, then we calculate the distance to th | frixinglife | NORMAL | 2023-01-12T10:20:23.956967+00:00 | 2023-01-15T10:03:36.893534+00:00 | 793 | false | # Intuition\n\n\n# Approach\nWe check for each point **the coincidence of at least one of the coordinates**. If there is a match, then we **calculate the distance to the original point**. If **the distance** turned out **to be less than it was**, then we **update it**, and **write the point index into the result**.\n\n# Complexity\n- Time complexity: $ O(n) $\n- Space complexity: $ O(1) $\n\n# Code\n```C++ []\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int result = -1, min_distance = INT_MAX;\n for (int i = 0; i < points.size(); i++) {\n int a = points[i][0], b = points[i][1];\n if (x == a || y == b) {\n int distance = abs(x - a) + abs(y - b);\n if (distance < min_distance) {\n min_distance = distance;\n result = i;\n }\n }\n }\n return result;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | easy c++ approach | easy-c-approach-by-sahildudi-i7rv | \n# Complexity\n- Time complexity:\no(n)\n\n\n\n# Code\n\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n | SahilDudi | NORMAL | 2022-12-19T07:50:55.035720+00:00 | 2022-12-19T07:50:55.035762+00:00 | 498 | false | \n# Complexity\n- Time complexity:\no(n)\n\n\n\n# Code\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int mindis=INT_MAX;\n int ans=-1;\n for(int i=0;i<points.size();i++){\n if(points[i][0]==x || points[i][1]==y){\n int dis=abs(x-points[i][0])+abs(y-points[i][1]);\n if(dis<mindis){\n mindis=dis;\n ans=i;\n \n }\n \n }\n }\n return ans;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy C++ Solution β | easy-c-solution-by-akanksha984-9ypn | Here is my C++ Solution :-\n\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int val= -1; int v= INT | akanksha984 | NORMAL | 2022-11-20T15:53:58.281154+00:00 | 2022-11-20T15:53:58.281195+00:00 | 656 | false | Here is my C++ Solution :-\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int val= -1; int v= INT_MAX;\n for (int i=0; i<points.size(); i++){\n if (x!=points[i][0] && y!= points[i][1])continue;\n if (v > (abs(points[i][0]-x)+abs(points[i][1]-y))){\n val= i; \n v=(abs(points[i][0]-x)+abs(points[i][1]-y));}\n }\n return val;\n }\n};\n``` | 2 | 0 | ['Array', 'Math', 'C', 'C++'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java | 2 Solutions | java-2-solutions-by-tbekpro-mpwn | Solution 1 | 13 ms\n\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int result = -1, min = Integer.MAX_VALUE;\n | tbekpro | NORMAL | 2022-11-02T04:34:16.521863+00:00 | 2022-11-02T04:34:16.521906+00:00 | 619 | false | # Solution 1 | 13 ms\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int result = -1, min = Integer.MAX_VALUE;\n for (int i = 0; i < points.length; i++) {\n if (x == points[i][0] || y == points[i][1]) {\n int manh = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);\n if (min > manh) {\n min = manh;\n result = i;\n }\n }\n }\n return result;\n }\n}\n```\n\n\n# Solution 2 | 6 ms\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int result = -1, minSum = Integer.MAX_VALUE;\n for (int i = 0; i < points.length; i++) {\n if (points[i][0] == x) {\n int manh = Math.abs(y - points[i][1]);\n if (minSum > manh) {\n minSum = manh;\n result = i;\n }\n } else if (points[i][1] == y) {\n int manh = Math.abs(x - points[i][0]);\n if (minSum > manh) {\n minSum = manh;\n result = i;\n }\n }\n }\n return result;\n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Simple Java Solution | simple-java-solution-by-abhi1234689-jjmm | \n\n# Code\n\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n\n int min = Integer.MAX_VALUE,index=-1;\n for(i | abhi1234689 | NORMAL | 2022-10-24T17:27:44.499893+00:00 | 2022-10-24T17:27:44.499926+00:00 | 368 | false | \n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n\n int min = Integer.MAX_VALUE,index=-1;\n for(int i=0;i<points.length;i++){\n int dist = Math.abs(points[i][0]-x) + Math.abs(points[i][1]-y);\n if((points[i][0] == x || points[i][1] == y) && min > dist){\n // if(min > dist){\n min = dist;\n index=i;\n // }\n }\n } \n return index; \n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | C++ Solution for beginners | c-solution-for-beginners-by-akshat0610-3fa8 | \nclass Solution {\npublic:\n int nearestValidPoint(int x1, int y1, vector<vector<int>>& arr) {\n \n int ans_dis=INT_MAX;\n int ans_idx= | akshat0610 | NORMAL | 2022-10-01T14:33:03.672270+00:00 | 2022-10-01T14:33:03.672305+00:00 | 506 | false | ```\nclass Solution {\npublic:\n int nearestValidPoint(int x1, int y1, vector<vector<int>>& arr) {\n \n int ans_dis=INT_MAX;\n int ans_idx=-1;\n\n for(int i=0;i<arr.size();i++)\n {\n int x2=arr[i][0];\n int y2=arr[i][1];\n\n if(x1==x2 or y1==y2)\n {\t\n int dis = abs(x1 - x2) + abs(y1 - y2);\n\n if(dis < ans_dis)\n {\n ans_dis = dis;\n ans_idx=i;\t\n }\t\n }\n else\n continue;\n }\n return ans_idx;\n }\n};\n``` | 2 | 0 | ['Array', 'C', 'C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | C++ Solution for beginners | c-solution-for-beginners-by-nehagupta_09-e7q7 | \nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int ans=-1;\n int dis;\n int min_dis= | NehaGupta_09 | NORMAL | 2022-10-01T14:26:41.397182+00:00 | 2022-10-01T14:26:41.397222+00:00 | 771 | false | ```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int ans=-1;\n int dis;\n int min_dis=INT_MAX;\n for(int i=0;i<points.size();i++)\n {\n if(points[i][0]==x or points[i][1]==y)\n {\n dis=abs(x-points[i][0])+abs(y-points[i][1]);\n if(dis<min_dis)\n {\n min_dis=dis;\n ans=i;\n }\n }\n }\n return ans;\n }\n};\n``` | 2 | 0 | ['C', 'C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java | Math | Array | Easy Solution | java-math-array-easy-solution-by-divyans-8e27 | \nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int min = Integer.MAX_VALUE,index=0;\n for(int i=0;i<points | Divyansh__26 | NORMAL | 2022-09-14T09:54:00.541595+00:00 | 2022-09-14T09:54:00.541634+00:00 | 116 | false | ```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int min = Integer.MAX_VALUE,index=0;\n for(int i=0;i<points.length;i++){\n int x1 = points[i][0];\n int y1 = points[i][1];\n if(x==x1 || y==y1){\n if(Math.abs(x-x1)+Math.abs(y-y1)<min){\n min=Math.abs(x-x1)+Math.abs(y-y1);\n index=i;\n }\n }\n }\n if(min==Integer.MAX_VALUE)\n return -1;\n return index;\n }\n}\n```\nKindly upvote if you like the code. | 2 | 0 | ['Array', 'Math', 'Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | JAVA | EASY CODE 3ms Solution | java-easy-code-3ms-solution-by-fardeensh-8kqa | \n int min=Integer.MAX_VALUE;\n int index=-1;\n int dist=0;\n for(int i=0;i<points.length;i++){\n if(points[i][0]==x||poi | fardeenshaik927 | NORMAL | 2022-08-20T19:52:31.650425+00:00 | 2022-08-20T19:53:09.811646+00:00 | 62 | false | \n int min=Integer.MAX_VALUE;\n int index=-1;\n int dist=0;\n for(int i=0;i<points.length;i++){\n if(points[i][0]==x||points[i][1]==y){\n dist = Math.abs(points[i][0]-x) + Math.abs(points[i][1]-y);\n if(dist<min){\n min=dist;\n index=i;\n }\n }\n \n }\n return index;\n | 2 | 0 | [] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [JAVA] || goto approach, simple and explained | java-goto-approach-simple-and-explained-447pr | Algorithm explained\nTo keep the runtime low we check for valid points and the closest one in a single run through (loop).\n\n\nclass Solution {\n public int | Arcta | NORMAL | 2022-08-15T12:35:37.243643+00:00 | 2022-08-15T12:35:37.243669+00:00 | 101 | false | **Algorithm explained**\nTo keep the runtime low we check for valid points and the closest one in a single run through (loop).\n\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n\n int minDist=Integer.MAX_VALUE; //the variable to keep track of the minimal distance so far\n int currentDist=0; //the current distance between our point and the current valid point from points[][]\n\t\tint index=-1; //the index of the future valid point with minimal distance (-1 if there are no valid points)\n \n for(int i=0; i<points.length; i++){\n\t\t\tif(points[i][0]==x||points[i][1]==y){ //if either x or y-coordinate are the same the point is valid\n \n currentDist=Math.abs(x-points[i][0])+Math.abs(y-points[i][1]); //calculate distance\n \n if(currentDist<minDist){minDist=currentDist; index=i;} //check if its lower than the previous low\n } \n \n }\n //if we ran through the entire we mustve found the right point \n return index; \n }\n}\n``` | 2 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python || Easy solution | python-easy-solution-by-e_stanulevich-e38v | ```\nind = -1\ndist = float(\'inf\')\n \nfor i, point in enumerate(points):\n\tif (x == point[0] or y == point[1]):\n\t\td = abs(x-point[0]) + abs(y-point | e_stanulevich | NORMAL | 2022-07-30T17:56:12.206055+00:00 | 2022-07-30T17:56:12.206101+00:00 | 217 | false | ```\nind = -1\ndist = float(\'inf\')\n \nfor i, point in enumerate(points):\n\tif (x == point[0] or y == point[1]):\n\t\td = abs(x-point[0]) + abs(y-point[1])\n if d < dist:\n\t\t\tdist = d\n ind = i\n \n\t\tif dist == 0:\n\t\t\tbreak\n \nreturn ind\n | 2 | 0 | ['Python'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Kotlin Fast and Easy Solution || 488ms, faster than 92.59% | kotlin-fast-and-easy-solution-488ms-fast-65hd | \nclass Solution {\n fun nearestValidPoint(x: Int, y: Int, points: Array<IntArray>): Int {\n var min = Int.MAX_VALUE\n var index = -1\n | Z3ROsum | NORMAL | 2022-07-19T01:32:33.250395+00:00 | 2022-07-19T01:32:33.250423+00:00 | 95 | false | ```\nclass Solution {\n fun nearestValidPoint(x: Int, y: Int, points: Array<IntArray>): Int {\n var min = Int.MAX_VALUE\n var index = -1\n for (ix in points.indices) {\n val i = points[ix]\n if (i[0] != x && i[1] != y) continue // the point is not "valid"\n val dist = Math.abs(i[0] - x) + Math.abs(i[1] - y)\n if (dist < min) { // update min and index\n min = dist\n index = ix\n }\n }\n return index // done\n }\n}\n``` | 2 | 0 | ['Kotlin'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Go one pass easy hehe | go-one-pass-easy-hehe-by-tuanbieber-u0df | \nfunc nearestValidPoint(x int, y int, points [][]int) int {\n var res int = -1\n \n smallestDistance := 1 << 63 - 1\n \n for i := 0; i < len(poi | tuanbieber | NORMAL | 2022-06-28T03:43:16.382219+00:00 | 2022-06-28T03:43:16.382249+00:00 | 232 | false | ```\nfunc nearestValidPoint(x int, y int, points [][]int) int {\n var res int = -1\n \n smallestDistance := 1 << 63 - 1\n \n for i := 0; i < len(points); i++ {\n if points[i][0] == x || points[i][1] == y {\n if distance([]int{x, y}, points[i]) < smallestDistance {\n smallestDistance = distance([]int{x, y}, points[i])\n res = i\n }\n }\n }\n \n return res\n}\n\nfunc distance(a []int, b[]int) int {\n return (a[0] - b[0])*(a[0] - b[0]) + (a[1] - b[1])*(a[1] - b[1])\n}\n``` | 2 | 0 | ['C', 'Python', 'Java', 'Go'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Simple Java Solution | simple-java-solution-by-kashish905-d8xm | \nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int smallest = Integer.MAX_VALUE ;\n int k = -1;\n | Kashish905 | NORMAL | 2022-06-16T09:14:24.784157+00:00 | 2022-06-16T09:14:24.784184+00:00 | 83 | false | ```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int smallest = Integer.MAX_VALUE ;\n int k = -1;\n for(int i = 0;i<points.length;i++){\n if(points[i][0]==x || points[i][1]==y ){\n if( Math.abs((points[i][0]-x)) + Math.abs((points[i][1]-y))<smallest){\n smallest = Math.abs((points[i][0]-x)) + Math.abs((points[i][1]-y));\n k=i;\n }\n }\n }\n return k;\n }\n}\n```\n``` If You Found This Helpful Then Do UpVote Plz``` | 2 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Find Nearest Point that same X or y coordinate | O(n) | find-nearest-point-that-same-x-or-y-coor-uoyb | I know this Looks so long but bare with me You will understand it thoroughly.\n\n\nclass Solution {\n \nprivate:\n int x1=0,y1=0; // To Store give x and y | babasaheb256 | NORMAL | 2022-05-27T07:59:08.525216+00:00 | 2022-05-27T07:59:38.973376+00:00 | 155 | false | I know this Looks so long but bare with me You will understand it thoroughly.\n```\n\nclass Solution {\n \nprivate:\n int x1=0,y1=0; // To Store give x and y val at start (to decrease manHatDis function memory footprint)\n // So that when calling function multiple time we did n\'t need to pass x and y val again and again\npublic:\n \n int manHatDis(int x2,int y2) // To calculate Manhattan Distance between (x1,y1) (x2,y2)\n {\n return (abs(x1-x2)+abs(y1-y2));\n }\n \n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n \n x1=x;\n y1=y;// [[1,2],[3,1],[2,4],[2,3],[4,4]]\n \n unordered_map<int,vector<int>> manhat; // To store Distance value as key (ManHattanDistance)\n // For each Manhattan Dis we store x2,y2,index for which this Manhattan Dis came\n // Here index is index of point (x2,y2) in Points array\n // eg: for point [2,4] index= 2 || for point [3,1] index= 1\n int index=0;\n \n for(auto point : points)\n {\n if(point[0]==x1 || point[1]==y1 ) // if point contain x1 or y1 then only proceed\n {\n int currDis= manHatDis(point[0],point[1]); // calculate Man Dis for current point\n \n if( manhat.find(currDis)!=manhat.end()) // if this Man Dis already present then \n {\n if(point[0]+point[1] < manhat[currDis][0]+manhat[currDis][1] ) // check if curr point (x+y) < prev store (x+y)\n {\n manhat[currDis][0]= point[0]; // store x2\n manhat[currDis][1]= point[1]; // store y2\n manhat[currDis][2]= index; // update latest index related to (x2,y2)\n }\n }\n else // if Man Dis not already present then Add fresh\n {\n manhat[currDis].push_back(point[0]); \n manhat[currDis].push_back(point[1]);\n manhat[currDis].push_back(index);\n }\n }\n \n index++; \n \n }\n \n \n int minDis=INT_MAX; // Intialize with MAX\n \n for(auto currDis: manhat) // To find min of all Man Distances Stored so far\n {\n if(currDis.first < minDis)\n minDis= currDis.first;\n }\n \n return minDis==INT_MAX?-1:manhat[minDis][2]; // if not found return -1 else return found Man Dis index\n \n }\n};\n``` | 2 | 0 | ['C', 'C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python - Pretty Brute Forceish but easy to understand | python-pretty-brute-forceish-but-easy-to-kev9 | \nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n valid_pair = [] | derrjohn | NORMAL | 2022-04-29T06:15:11.560562+00:00 | 2022-04-29T06:15:11.560589+00:00 | 244 | false | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n valid_pair = [] #Initializing an empty array\n distance_holder = (9999,9999) #creating default values for distance holder \n\n \n def __calculate_manhattan(x,y,x_point,y_point): #helper function to calculate manhattan distance\n return abs(x-x_point) + abs(y-y_point)\n \n for idx,pair in enumerate(points): #iterate through the points and keep index\n x_point,y_point = pair #unpack pairs into x/y points\n \n if x==x_point or y==y_point: #checking if x or y equal points\n distance = __calculate_manhattan(x,y,x_point,y_point) #get manhattan distance\n if distance <= distance_holder[1]: #check if distance is less than what\'s currently in holder\n if distance == distance_holder[1]: #check if distances are equal to each other\n distance_holder = (min(distance_holder[0],idx),distance) #if distances are equal only use minimum index\n else:\n distance_holder = (idx,distance) #update distance holder\n valid_pair.append(distance_holder) #this was a remnant of brainstorming ways to solve problem , would need to refactor logic to remove this \n \n if not valid_pair: #checks if any elements are in valid pair\n return -1\n else:\n return distance_holder[0] #returns index\n\t\t\t``` | 2 | 0 | ['Python', 'Python3'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy to understand - Python 3 | easy-to-understand-python-3-by-ankit_100-0ywp | class Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n \n min_d=1000000\n i=0\n ans=-1\ | ankit_100 | NORMAL | 2022-03-24T06:16:37.937921+00:00 | 2022-03-24T06:16:37.937957+00:00 | 76 | false | class Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n \n min_d=1000000\n i=0\n ans=-1\n for point in points:\n if x==point[0] or y == point[1]:\n mah = abs(x-point[0]) + abs(y-point[1])\n if mah<min_d:\n min_d=mah\n ans=i\n i+=1\n return ans\n \n | 2 | 0 | [] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python3 memory usage less than 94.85% | python3-memory-usage-less-than-9485-by-e-rihf | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n ind = -1\n man = -1\n for i in poi | elefant1805 | NORMAL | 2022-03-15T12:29:53.831386+00:00 | 2022-03-15T12:29:53.831413+00:00 | 300 | false | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n ind = -1\n man = -1\n for i in points:\n if i[0] == x or i[1] == y:\n if man == -1 or (abs(i[0] - x) + abs(i[1] - y)) < man:\n man = abs(i[0] - x) + abs(i[1] - y)\n ind = points.index(i)\n return ind | 2 | 0 | ['Python3'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python Easy and Small Solution | python-easy-and-small-solution-by-imprad-9fi7 | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n \n li=[i for i in points if i[0]==x or i[ | imPradhyumn | NORMAL | 2022-03-09T17:19:20.024519+00:00 | 2022-03-09T17:19:20.024561+00:00 | 194 | false | ```\nclass Solution:\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n \n li=[i for i in points if i[0]==x or i[1]==y] #chosse points where x or y is same\n if(len(li)==0): \n return -1\n ans=0\n mn=float(\'inf\') #it is same as in c++ -> int_max\n for i in li:\n dist=abs(x-i[0])+abs(y-i[1])\n if(mn>dist):\n ans=i\n mn=dist \n return points.index(ans)\n \n | 2 | 0 | ['Python'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | C++ | O(n) Time complexity | FASTER THAN 96.04% | c-on-time-complexity-faster-than-9604-by-v5rc | \nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int minimum_manhattan = INT_MAX;\n int index | karnaa | NORMAL | 2022-03-03T11:21:13.692018+00:00 | 2022-03-03T11:21:13.692051+00:00 | 160 | false | ```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int minimum_manhattan = INT_MAX;\n int index = -1;\n for (int i = 0; i < points.size(); i++) {\n if(points[i][0] == x || points[i][1] == y) {\n int manhattan = abs(points[i][0] - x) + abs(points[i][1] - y);\n if (manhattan < minimum_manhattan) {\n minimum_manhattan = manhattan;\n index = i;\n }\n }\n }\n return index;\n }\n};\n``` | 2 | 0 | ['C'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python O(N) time O(1) space | python-on-time-o1-space-by-nybblr-57cq | python\nclass Solution:\n\t"""\n\tIn this problem we will simply iterate over all the points and keep track of the point \n\twith the lowest Manhattan distance. | nybblr | NORMAL | 2021-08-26T01:56:33.440169+00:00 | 2021-08-26T01:56:33.440222+00:00 | 189 | false | ```python\nclass Solution:\n\t"""\n\tIn this problem we will simply iterate over all the points and keep track of the point \n\twith the lowest Manhattan distance. We only perform this check if either x,y of the point\n\tmatches our current x,y from the function.\n\t"""\n def getManhattanDistance(self, point1: [int], point2: [int]):\n x, y = point1\n x2, y2 = point2\n return abs(x-x2) + abs(y - y2)\n\n def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:\n min_manhattan_distance = float("inf")\n min_manhattan_distance_index = -1\n for i in range(len(points)):\n point_x, point_y = points[i]\n if point_x == x or point_y == y:\n current_manhattan_distance = self.getManhattanDistance([x, y], points[i])\n if current_manhattan_distance < min_manhattan_distance:\n min_manhattan_distance = current_manhattan_distance\n min_manhattan_distance_index = i\n \n return min_manhattan_distance_index\n``` | 2 | 0 | ['Python'] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Simple JAVA 2MS answer | simple-java-2ms-answer-by-ranjan_1997-lvdq | \nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int result = -1;\n int distance = Integer.MAX_VALUE;\n | ranjan_1997 | NORMAL | 2021-07-26T05:49:47.725764+00:00 | 2021-07-26T05:49:47.725810+00:00 | 147 | false | ```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int result = -1;\n int distance = Integer.MAX_VALUE;\n for(int i = 0;i<points.length;i++)\n { \n int x1 = points[i][0];\n int y1 = points[i][1];\n if(x1 == x || y1 == y)\n { \n int temp = Math.abs(x1-x) + Math.abs(y1-y); \n if(distance > temp)\n { \n distance = temp;\n result = i;\n }\n } \n \n }\n return result;\n }\n}\n``` | 2 | 0 | [] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Javascript one-line | javascript-one-line-by-saynn-f0cm | \n/**\n * @param {number} x\n * @param {number} y\n * @param {number[][]} points\n * @return {number}\n */\nvar nearestValidPoint = function(x, y, points) {\n | saynn | NORMAL | 2021-04-04T11:52:50.069715+00:00 | 2021-04-04T11:52:50.069745+00:00 | 214 | false | ```\n/**\n * @param {number} x\n * @param {number} y\n * @param {number[][]} points\n * @return {number}\n */\nvar nearestValidPoint = function(x, y, points) {\n return points.findIndex(p => p === points.filter(p => p[0] === x || p[1] === y)\n .sort((a,b) => -Math.abs(b[1]-y + b[0]-x) + Math.abs(a[1]-y + a[0]-x))[0])\n};\n``` | 2 | 0 | ['JavaScript'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java PriorityQueue :) | java-priorityqueue-by-rrosy2000-ubz2 | \nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n \n //If the distance is same then sort using index else dis | rrosy2000 | NORMAL | 2021-03-27T12:24:59.795218+00:00 | 2021-03-27T12:24:59.795248+00:00 | 98 | false | ```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n \n //If the distance is same then sort using index else distance;\n PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> a[1]==b[1] ? a[0]-b[0] : a[1]-b[1]);\n \n for(int i=0;i<points.length;i++)\n {\n if(points[i][0]==x || points[i][1]==y)\n {\n int[] temp = new int[2];\n temp[0] = i;\n int distance = Math.abs(points[i][0]-x) + Math.abs(points[i][1]-y);\n temp[1] = distance;\n pq.add(temp);\n }\n }\n \n //NO valid index.\n if(pq.isEmpty())\n return -1;\n else\n {\n return pq.poll()[0]; //Returns the index of the fisrt element that has the least manhatt distance.\n }\n \n }\n}\n\n\n``` | 2 | 0 | [] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | C# | c-by-replicant-092v | One pass O(N)\n\npublic class Solution\n{\n public int NearestValidPoint(int x, int y, int[][] points)\n {\n int minIdx = -1;\n int minDista | replicant | NORMAL | 2021-03-08T08:35:03.636799+00:00 | 2021-03-08T08:35:03.636834+00:00 | 128 | false | One pass O(N)\n```\npublic class Solution\n{\n public int NearestValidPoint(int x, int y, int[][] points)\n {\n int minIdx = -1;\n int minDistance = int.MaxValue;\n for (int i = 0; i < points.Length; ++i)\n {\n var point = points[i];\n int distance;\n // No need to calculate the sum, since one of the coordinates is the same and the difference is zero\n if (point[0] == x)\n distance = Math.Abs(point[1] - y);\n else if (point[1] == y)\n distance = Math.Abs(point[0] - x);\n else // point[0] != x && point[1] != y therefore not valid\n continue;\n if (distance < minDistance)\n {\n minDistance = distance;\n minIdx = i;\n }\n }\n return minIdx;\n }\n}\n```\n\nAnd using Linq just for the heck of it. Yes, it\'s slower since it involves OrderBy, just as an exercise.\n```\npublic class Solution\n{\n public int NearestValidPoint(int x, int y, int[][] points)\n {\n return points\n .Select((point, idx) => (Idx: idx, X: point[0], Y: point[1], Distance: Math.Abs(point[0] - x) + Math.Abs(point[1] - y)))\n .Where(point => point.X == x || point.Y == y)\n .OrderBy(point => point.Distance)\n .ThenBy(point => point.Idx)\n\t\t\t.Select(point => point.Idx)\n\t\t\t.DefaultIfEmpty(-1)\n\t\t\t.First();\n }\n}\n``` | 2 | 0 | [] | 1 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | 100% easy beat || O(N) sol. || beginner friendly sol. || | 100-easy-beat-on-sol-beginner-friendly-s-zzfs | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | iitian_010u | NORMAL | 2025-03-05T10:50:10.864959+00:00 | 2025-03-05T10:50:10.864959+00:00 | 160 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
int mini_dis = INT_MAX, ans = -1;
for(int i = 0; i < points.size(); i++) {
int a = points[i][0], b = points[i][1];
if(a == x || b == y) {
int current = abs(a - x) + abs(b - y);
if(current < mini_dis) {
mini_dis = current;
ans = i;
}
}
}
return ans;
}
};
``` | 1 | 0 | ['Array', 'Python', 'C++', 'Python3'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Python 3 π || Beats 100% π || Easy π₯ | python-3-beats-100-easy-by-rudraksh25-jngw | IntuitionThe problem requires finding the closest valid point that shares either the same x or y coordinate with our current location. Since Manhattan distance | Rudraksh25 | NORMAL | 2025-02-09T08:46:32.437226+00:00 | 2025-02-09T08:46:32.437226+00:00 | 119 | false | 
# 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
``` | 1 | 0 | ['Python3'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | O(n) π€© π₯ | on-by-varuntyagig-7uom | Complexity
Time complexity:
O(n)
Space complexity:
O(1)
Code | varuntyagig | NORMAL | 2025-01-26T10:35:55.321381+00:00 | 2025-01-26T10:35:55.321381+00:00 | 60 | false | 
# Complexity
- Time complexity:
$$O(n)$$
- Space complexity:
$$O(1)$$
# Code
```cpp []
class Solution {
public:
int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
int min = INT_MAX, index = -1;
for (int i = 0; i < points.size(); i++) {
int xi = points[i][0];
int yi = points[i][1];
// valid points
if ((x == xi) || (y == yi)) {
int distance = abs(x - xi) + abs(y - yi);
// find index
if (min > distance) {
min = distance;
index = i;
}
}
}
return index;
}
};
``` | 1 | 0 | ['Array', 'Math', 'C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | βοΈ Finding Nearest Point That Has the Same X or Y Coordinate. βοΈ | finding-nearest-point-that-has-the-same-8z100 | Code | Abdusalom_16 | NORMAL | 2024-12-23T05:34:27.075631+00:00 | 2024-12-23T05:34:27.075631+00:00 | 38 | false | # Code
```dart []
class Solution {
int nearestValidPoint(int x, int y, List<List<int>> points) {
List<int> distances = [];
for(int i = 0; i < points.length; i++){
if(points[i][0] == x || points[i][1] == y){
distances.add((x-points[i][0]).abs() + (y-points[i][1]).abs());
}
}
distances.sort((a, b) => a.compareTo(b));
if(distances.isEmpty){
return -1;
}
int smallestNumber = distances.first;
print(smallestNumber);
for(int i = 0; i < points.length; i++){
if(smallestNumber == ((x-points[i][0]).abs() + (y-points[i][1]).abs()) && points[i][0] == x || points[i][1] == y && smallestNumber == ((x-points[i][0]).abs() + (y-points[i][1]).abs())){
return i;
}
}
return -1;
}
}
``` | 1 | 0 | ['Array', 'Dart'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | long turn leke short turn aayi ismein ho gya wahi imp bas :D | long-turn-leke-short-turn-aayi-ismein-ho-5n2p | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | yesyesem | NORMAL | 2024-09-05T10:43:25.712717+00:00 | 2024-09-05T10:43:25.712747+00:00 | 13 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n\n vector<pair<int,int>>vec;\n for(int i=0;i<points.size();i++)\n {\n //if(points[i][0]==x||points[i][1]==y)\n vec.push_back({points[i][0],points[i][1]});\n \n }\n\n int min=INT_MAX;\n int dist;\n int ind=-1;\n\n for(int i=0;i<vec.size();i++)\n {\n if(vec[i].first==x||vec[i].second==y)\n { dist=abs(vec[i].first-x)+abs(vec[i].second-y);\n if(dist<min)\n { min=dist;\n ind=i;\n }}\n }\n \n\n return ind;\n \n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy Solution Beats 100% | easy-solution-beats-100-by-vasu15-n46e | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Vasu15 | NORMAL | 2024-06-28T07:17:12.072359+00:00 | 2024-06-28T07:17:12.072409+00:00 | 240 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int ans=-1;\n int m=Integer.MAX_VALUE;\n for(int i=0;i<points.length;i++){\n if(points[i][0]==x || points[i][1]==y){\n int c=Math.min(points[i][0] , points[i][1]);\n if(x==points[i][0]){\n if(m>Math.abs(points[i][1]-y)) {m=Math.abs(points[i][1]-y);\n ans=i;}\n }\n else{\n if(m>Math.abs(points[i][0]-x)) {m=Math.abs(points[i][0]-x);\n ans=i;}\n }\n\n }\n }\n return ans;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Beats 100%π₯JAVA | Brute Force | beats-100java-brute-force-by-sumo25-bsxy | \n\n# Approach\n Describe your approach to solving the problem. \nBrute Force\n\n# Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n | sumo25 | NORMAL | 2024-02-16T09:52:15.051478+00:00 | 2024-02-16T09:52:15.051513+00:00 | 186 | false | \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBrute Force\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int ans=Integer.MAX_VALUE;\n int min=Integer.MAX_VALUE;\n int flag=0;\n for(int i=0;i<points.length;i++){\n if(points[i][0]==x || points[i][1]==y){\n if(min>Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y)){\n min=Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y);\n ans=i;\n }\n else if(min==Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y)){\n ans=Math.min(ans,i);\n }\n flag=1;\n }\n }\n if(flag==0){\n return -1;\n }\n return ans;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Simple java code 2 ms beats 99 % && 49 mb beats 84 % | simple-java-code-2-ms-beats-99-49-mb-bea-gd2k | \n# Complexity\n-\n\n# Code\n\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int ind=-1;\n int min=10001;\n | Arobh | NORMAL | 2024-01-14T04:54:14.147715+00:00 | 2024-01-14T04:54:14.147738+00:00 | 1 | false | \n# Complexity\n-\n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int ind=-1;\n int min=10001;\n for(int i=0;i<points.length;i++) {\n if(points[i][0]==x||points[i][1]==y) {\n int val=Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y);\n if(val<min){\n min=val;\n ind=i;\n }\n }\n }\n return ind;\n\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Find Nearest Point That Has the Same X or Y Coordinate | find-nearest-point-that-has-the-same-x-o-4ea5 | \n\n# Code\n\nclass Solution {\n public static class Points implements Comparable<Points>{\n int x,y,d,i;\n public Points(int x,int y,int d,int | riya1202 | NORMAL | 2023-10-24T09:29:37.420182+00:00 | 2023-10-24T09:29:37.420205+00:00 | 607 | false | \n\n# Code\n```\nclass Solution {\n public static class Points implements Comparable<Points>{\n int x,y,d,i;\n public Points(int x,int y,int d,int i){\n this.x=x;\n this.y=y;\n this.d=d;\n this.i=i;\n }\n public int compareTo(Points p2){\n if(this.d==p2.d)\n return this.i - p2.i;\n return this.d-p2.d;\n }\n }\n public int nearestValidPoint(int x, int y, int[][] points) {\n PriorityQueue<Points> pq=new PriorityQueue<>();\n for(int i=0;i<points.length;i++){\n if(points[i][0]!=x && points[i][1]!=y)\n continue;\n int d=Math.abs(x-points[i][0])+Math.abs(y-points[i][1]);\n pq.add(new Points(points[i][0], points[i][1], d,i));\n }\n if(pq.isEmpty())\n return -1;\n return pq.remove().i;\n }\n}\n``` | 1 | 0 | ['Array', 'Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy C++ solution || Beginner-friendly | easy-c-solution-beginner-friendly-by-pra-lh99 | \n\n# Code\n\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int ans=-1, min=INT_MAX;\n for(i | prathams29 | NORMAL | 2023-06-22T19:44:03.776738+00:00 | 2023-06-22T19:44:03.776759+00:00 | 18 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int ans=-1, min=INT_MAX;\n for(int i=0; i<points.size(); i++)\n {\n int a = points[i][0];\n int b = points[i][1];\n if(a==x || b==y)\n {\n int val = abs(x-a)+abs(y-b);\n if(val<min){\n ans=i;\n min=val;\n }\n }\n }\n return ans;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Simple JAVA Solution for beginners. 2ms. Beats 67.96%. | simple-java-solution-for-beginners-2ms-b-xm11 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | sohaebAhmed | NORMAL | 2023-05-10T02:40:42.545968+00:00 | 2023-05-10T02:40:42.546015+00:00 | 82 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int manhattanDistance;\n int smallestManhattanDistance = Integer.MAX_VALUE;\n int smallestManhattanDistanceIndex = -1;\n for(int i = 0; i < points.length; i++) {\n if(x == points[i][0] || y == points[i][1]) {\n manhattanDistance = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);\n if(manhattanDistance < smallestManhattanDistance) {\n smallestManhattanDistance = manhattanDistance;\n smallestManhattanDistanceIndex = i;\n }\n }\n }\n return smallestManhattanDistanceIndex;\n }\n}\n``` | 1 | 0 | ['Array', 'Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Find Nearest Point That Has the Same X or Y Coordinate Solution in C++ | find-nearest-point-that-has-the-same-x-o-mnyz | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | The_Kunal_Singh | NORMAL | 2023-03-27T14:33:14.862592+00:00 | 2023-03-27T14:33:14.862624+00:00 | 74 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n int i, ind=-1, distance, min_dis=100000;\n for(i=0 ; i<points.size() ; i++)\n {\n if(points[i][0]==x || points[i][1]==y)\n {\n distance = abs(points[i][0]-x) + abs(points[i][1]-y);\n if(distance<min_dis)\n {\n min_dis = distance;\n ind = i;\n }\n }\n }\n return ind;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy Java solution | easy-java-solution-by-kunal_kamble-9esf | \n# Code\n\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int index=-1;\n int min_distance=Integer.MAX_VALUE | kunal_kamble | NORMAL | 2023-03-19T09:52:28.471096+00:00 | 2023-03-19T09:52:28.471129+00:00 | 48 | false | \n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int index=-1;\n int min_distance=Integer.MAX_VALUE;\n for (int i = 0; i < points.length; i++) {\n if(points[i][0]==x || points[i][1]==y){\n int distance=Math.abs(points[i][0]-x)+Math.abs(points[i][1]-y);\n if(distance<min_distance){\n min_distance=distance;\n index=i;\n }\n }\n }\n\n return index; \n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Solved with using boolean array | solved-with-using-boolean-array-by-vikin-0hun | Intuition\n Describe your first thoughts on how to solve this problem. \nIn this question it is the main thing is \nVALID - Points having either x or y or both | viking09_ | NORMAL | 2023-03-06T10:59:25.458957+00:00 | 2023-03-06T10:59:25.458994+00:00 | 100 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn this question it is the main thing is \nVALID - Points having either x or y or both \nNOT VALID - Points dont having x or y\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSo will take an boolean array from which we will check its a valid point or not.\nValid point will be declared as true and non valid as false.\n\nThen we will travserse the array and store the minimum value in the variable and return that value;\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n# Code\n```\nclass Solution {\n public static int nearestValidPoint(int x, int y, int[][] points) {\n boolean flag[] = new boolean[points.length];\n int min = Integer.MAX_VALUE;\n int reqindex = -1;\n \n for (int i = 0; i < points.length; i++) {\n if (points[i][0] == x || points[i][1] == y) {\n flag[i] = true;\n }\n }\n \n for(int i=0; i<flag.length; i++){\n if(flag[i]){\n int value = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1]);\n if(value < min){\n min = value;\n reqindex = i;\n }\n }\n }\n return reqindex; \n } \n\n}\n``` | 1 | 0 | ['Array', 'Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | [Ruby] O(n) solution | ruby-on-solution-by-artemtrepalin-rij0 | Code\n\n# @param {Integer} x\n# @param {Integer} y\n# @param {Integer[][]} points\n# @return {Integer}\ndef nearest_valid_point(x, y, points)\n res = -1\n min | ArtemTrepalin_ | NORMAL | 2023-02-25T11:12:53.974107+00:00 | 2023-02-25T20:11:17.695285+00:00 | 78 | false | # Code\n```\n# @param {Integer} x\n# @param {Integer} y\n# @param {Integer[][]} points\n# @return {Integer}\ndef nearest_valid_point(x, y, points)\n res = -1\n min_md = 9999999\n\n points.each_with_index do |point, index|\n next unless x == point[0] || y == point[1]\n next unless min_md > (x - point[0]).abs + (y - point[1]).abs\n min_md = (x - point[0]).abs + (y - point[1]).abs\n res = index\n end\n\n res\nend\n``` | 1 | 0 | ['Ruby'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java&Javascript Solution (JW) | javajavascript-solution-jw-by-specter01w-m0bl | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | specter01wj | NORMAL | 2023-01-24T17:19:06.042290+00:00 | 2023-01-24T17:19:06.042333+00:00 | 245 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\nJava:\n```\npublic int nearestValidPoint(int x, int y, int[][] points) {\n int index = -1; \n for (int i = 0, smallest = Integer.MAX_VALUE; i < points.length; ++i) {\n int dx = x - points[i][0], dy = y - points[i][1];\n if (dx * dy == 0 && Math.abs(dy + dx) < smallest) {\n smallest = Math.abs(dx + dy);\n index = i;\n }\n }\n return index; \n}\n```\nJavascript:\n```\n/**\n * @param {number} x\n * @param {number} y\n * @param {number[][]} points\n * @return {number}\n */\nvar nearestValidPoint = function(x, y, points) {\n let index = -1; \n for (let i = 0, smallest = Number.MAX_VALUE; i < points.length; ++i) {\n let dx = x - points[i][0], dy = y - points[i][1];\n if (dx * dy === 0 && Math.abs(dy + dx) < smallest) {\n smallest = Math.abs(dx + dy);\n index = i;\n }\n }\n return index;\n};\n``` | 1 | 0 | ['Java', 'JavaScript'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Beats 97.73% | beats-9773-by-engeman08-v6m6 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | engeman08 | NORMAL | 2022-12-20T17:32:47.201079+00:00 | 2022-12-20T17:32:47.201109+00:00 | 42 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfunction nearestValidPoint(x: number, y: number, points: number[][]): number {\n\n let smallDistance = Number.MAX_SAFE_INTEGER;\n let smallIndex = -1;\n\n for(let i=0;i<points.length;i++){\n const distance = Math.abs(points[i][0] - x) \n + Math.abs(points[i][1] - y);\n\n if((points[i][0] === x || points[i][1] === y) && distance < smallDistance){\n smallDistance = distance;\n smallIndex = i;\n } \n }\n return smallIndex;\n};\n``` | 1 | 0 | ['TypeScript'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | JAVA SOLUTION 2ms runtime π | java-solution-2ms-runtime-by-harsh_singh-1q6c | Java Solution for \'Find Nearest Point That has the same X or Y Coordinate.\nRuntime - 2ms\nBeats - 95%\n\nclass Solution {\n public int nearestValidPoint(in | Harsh_Singh23_ | NORMAL | 2022-11-09T05:06:32.261214+00:00 | 2022-11-09T05:07:17.717477+00:00 | 13 | false | Java Solution for \'Find Nearest Point That has the same X or Y Coordinate.\nRuntime - 2ms\nBeats - 95%\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int result = -1, minSum = Integer.MAX_VALUE;\n for (int i = 0; i < points.length; i++) {\n if (points[i][0] == x) {\n int manh = Math.abs(y - points[i][1]);\n if (minSum > manh) {\n minSum = manh;\n result = i;\n }\n } else if (points[i][1] == y) {\n int manh = Math.abs(x - points[i][0]);\n if (minSum > manh) {\n minSum = manh;\n result = i;\n }\n }\n }\n return result;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | π [ JAVA ] β Faster than 100% β Simple explanation π’ | java-faster-than-100-simple-explanation-nvfit | Complexity:\nTime complexity: O(n)\nSpace complexity: O(1)\n\n\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n \n | TristanCopley | NORMAL | 2022-11-07T03:56:47.708367+00:00 | 2022-11-07T19:27:51.382686+00:00 | 16 | false | **Complexity:**\n*Time complexity: O(n)\nSpace complexity: O(1)*\n\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n \n int index = -1;\n int closest = Integer.MAX_VALUE;\n int[] point;\n \n for (int i = 0; i < points.length; i++) {\n \n point = points[i];\n \n if (point[0] == x || point[1] == y) {\n \n if (point[0] == x && point[1] == y) {\n \n return i;\n \n }\n \n int dist = Math.abs(point[0] - x) + Math.abs(point[1] - y);\n \n if (dist < closest) {\n \n closest = dist;\n index = i;\n \n }\n \n }\n \n }\n \n return index;\n \n }\n \n}\n```\n\n**Explanation:**\nFor this problem we can think of it in two parts. The first part is finding whether or not one of the `X` or `Y` of any given point matches the `X` and `Y` parameters. The second part is calculating the *Manhattan Distance* of the two points and returning the the first index of the nearest point.\n\nWe want to iterate over every point in the `points` array and check if either `X` or `Y` value is matching. If not we will skip to the next item. Next, we can check if the point matches exactly the `X` and `Y` parameters, in which case we can return early. If not, we calculate the Manhattan Distance and check if it is smaller than but NOT EQUAL TO the current closest distance. We then return the index.\n\n**If this solution was helpful in any way I would greatly appreciate an upvote. For solutions to other problems, feel free to click on my account as I have many more solutions on many different problems.** | 1 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | java | java-by-amitjha00-h797 | class Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int a =0 ,b =0;\n var maxdis = Integer.MAX_VALUE;\n in | amitjha00 | NORMAL | 2022-10-10T08:09:30.075290+00:00 | 2022-10-10T08:09:30.075336+00:00 | 25 | false | class Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int a =0 ,b =0;\n var maxdis = Integer.MAX_VALUE;\n int ans = -1;\n int sum =0;\n for(int i=0; i< points.length; i++){\n a = points[i][0];\n b = points[i][1];\n if(x==a || y == b){\n sum = Math.abs(x - a) + Math.abs(y-b);\n if(sum < maxdis){\n maxdis = sum;\n ans = i;\n }\n }\n \n }\n \n return ans;\n }\n} | 1 | 0 | ['Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | JS | js-by-deleted_user-ik2k | \nconst nearestValidPoint = (x, y, points) => {\n const result = { distance: Infinity, index: -1 }\n for (let i = 0; i < points.length; i++) {\n if | deleted_user | NORMAL | 2022-10-09T07:12:22.962434+00:00 | 2022-10-09T07:12:22.962477+00:00 | 459 | false | ```\nconst nearestValidPoint = (x, y, points) => {\n const result = { distance: Infinity, index: -1 }\n for (let i = 0; i < points.length; i++) {\n if (points[i][0] === x || points[i][1] === y) {\n const calc = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1])\n if (calc < result.distance) {\n result.distance = calc\n result.index = i\n }\n }\n }\n return result.index\n}\n``` | 1 | 0 | [] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy Solution in Java for Beginners | easy-solution-in-java-for-beginners-by-h-dio1 | Intuition\n Describe your first thoughts on how to solve this problem. \nBruteforce\n\n# Approach\n Describe your approach to solving the problem. \nWe want A p | hashcoderz | NORMAL | 2022-10-07T06:11:56.527958+00:00 | 2022-10-07T06:11:56.527999+00:00 | 303 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBruteforce\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe want A point that shares the same x-coordinate or the same y-coordinate as your location, dx * dy == 0 indicate either dx equals zero or dy equals zero, so we can make the product of dx and dy to be zero. dx and dy means the difference of x-coordinate and y-coordinate respectively. If the difference is zero, then they must be equal or shares the same x/y-coordinate.\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int min = 10000;\n int index = -1;\n for(int i =0 ; i<points.length; i++){\n int dx = x - points[i][0];\n int dy = y - points[i][1];\n if(dx*dy==0 && Math.abs(dx+dy)<min){\n min = Math.abs(dx+dy);\n index =i;\n }\n }\n return index;\n }\n}\n``` | 1 | 0 | ['Array', 'Java'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Easy JavaScript with Comments | easy-javascript-with-comments-by-pratikv-c4tq | \nvar nearestValidPoint = function(x, y, points) {\n var distance=Infinity; //To keep track of smallest distance found\n var index=-1; //To keep track | pratikvpatil2002 | NORMAL | 2022-09-30T12:26:57.984364+00:00 | 2022-09-30T12:26:57.984405+00:00 | 17 | false | ```\nvar nearestValidPoint = function(x, y, points) {\n var distance=Infinity; //To keep track of smallest distance found\n var index=-1; //To keep track of index\n for(let i=0;i<points.length;i++){\n //If points are valid\n if(points[i][0]==x || points[i][1]==y){ \n //Calutating the current distance\n let current_distance=Math.abs(points[i][0]-x) + Math.abs(points[i][1]-y); \n //If current distance is less than smallest distance found till now then we update the index and distance value\n if(current_distance<distance)\n {\n distance=current_distance;\n index=i;\n }\n }\n }\n //If no point is found we by default return -1\n return index; \n};\n``` | 1 | 0 | [] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | π©βπ» Javascript: Arrow function with reduce O(N) | javascript-arrow-function-with-reduce-on-f4nl | \nconst nearestValidPoint = (x, y, points, min = Infinity) => \n points.reduce((res, [a,b], idx) => {\n if(a===x || b===y){\n const dist = | prajaktashirke20 | NORMAL | 2022-09-18T04:40:19.990987+00:00 | 2022-09-18T04:40:19.991019+00:00 | 304 | false | ```\nconst nearestValidPoint = (x, y, points, min = Infinity) => \n points.reduce((res, [a,b], idx) => {\n if(a===x || b===y){\n const dist = Math.abs(x-a) + Math.abs(y-b)\n if(dist<min){\n res = idx;\n min = dist;\n }\n }\n return res\n }, -1)\n```\n\nOpen for suggestion | 1 | 0 | ['JavaScript'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | With Explanation Comments: Time: 342 ms (23.70%), Space: 59.4 MB (73.79%) | with-explanation-comments-time-342-ms-23-kydn | Like it? ->Upvote please! \u30C4\n\n\'\'\'\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector>& points) {\n \n //initializ | deleted_user | NORMAL | 2022-09-17T19:28:14.047955+00:00 | 2022-09-17T19:28:14.048000+00:00 | 323 | false | **Like it? ->Upvote please!** \u30C4\n\n\'\'\'\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& points) {\n \n //initialize a position & minimum possible absolute value & calculated distance variables\n int minValue=INT_MAX, dis=0, index=-1;\n \n //loop over the whole array elements\n for(int i=0;i<points.size();i++){\n \n //check if the x-coordinates are equals or the y ones\n if(points[i][0]==x || points[i][1]==y){\n //calculate the manhattan distance between the two points\n dis=abs(points[i][0]-x)+abs(points[i][1]-y); \n \n //check if the current distance is smallest reached one\n if(dis<minValue){\n //if no-> save the lowest value\n minValue=dis;\n //also, the current updated index\n index=i;\n }\n }\n }\n \n //return the index & if there\'s no one-> return -1 as it initialized before\n return index;\n }\n};\n\'\'\'\n\n**Like it? ->Upvote please!** \u30C4\n**If still not understood, feel free to comment. I will help you out**\n**Happy Coding :)** | 1 | 0 | ['Array', 'C', 'C++'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | C++||Easy to Understand | ceasy-to-understand-by-return_7-ft7l | ```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector>& p) \n {\n int ans=INT_MAX,res=-1;\n for(int i=0;iabs(x-p[i][0] | return_7 | NORMAL | 2022-09-15T19:31:58.453298+00:00 | 2022-09-15T19:31:58.453338+00:00 | 131 | false | ```\nclass Solution {\npublic:\n int nearestValidPoint(int x, int y, vector<vector<int>>& p) \n {\n int ans=INT_MAX,res=-1;\n for(int i=0;i<p.size();i++)\n {\n if(x==p[i][0]||y==p[i][1])\n {\n if(ans>abs(x-p[i][0])+abs(y-p[i][1]))\n {\n res=i;\n ans=abs(x-p[i][0])+abs(y-p[i][1]);\n }\n }\n }\n return res;\n \n }\n};\n//if you like the solution plz upvote. | 1 | 0 | ['C'] | 0 |
find-nearest-point-that-has-the-same-x-or-y-coordinate | Java Solution 1ms 100% faster | java-solution-1ms-100-faster-by-mahmouds-5dxx | \nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int [] solution = {0,Integer.MAX_VALUE};\n int index = 0;\ | MahmoudSafan | NORMAL | 2022-09-12T07:57:11.911867+00:00 | 2022-09-12T07:57:46.447804+00:00 | 92 | false | ```\nclass Solution {\n public int nearestValidPoint(int x, int y, int[][] points) {\n int [] solution = {0,Integer.MAX_VALUE};\n int index = 0;\n for(int[] point:points){\n if(x == point[0] || y == point[1]){\n int sum = Math.abs(x - point[0]) + Math.abs(y - point[1]);\n if (sum < solution[1]){\n solution[0] = index;\n solution[1] = sum;\n }\n }\n index++;\n }\n return (solution[1] == Integer.MAX_VALUE) ? -1 : solution[0];\n }\n}\n``` | 1 | 0 | ['Array'] | 0 |
maximum-coins-from-k-consecutive-bags | [Java/C++/Python] Sliding Window | javacpython-sliding-window-by-lee215-3418 | IntuitionThe interval to pick will either/or
Start at A[i][0]
End at A[i][1]
ExplanationOne sliding window to check the interval starting at A[i][0].
One slidin | lee215 | NORMAL | 2025-01-05T04:21:19.900674+00:00 | 2025-01-05T07:02:43.800112+00:00 | 7,613 | false | # **Intuition**
The interval to pick will either/or
- Start at A[i][0]
- End at A[i][1]
# **Explanation**
One sliding window to check the interval starting at `A[i][0]`.
One sliding window to check the interval ending at `A[i][0]`.
`cur` is the `coins` from `k` consecutive bags.
`part` need to calculate if the edge interval is overlapped with `k` bags.
return the bigger result from two passes.
# **Complexity**
Sort
Time `O(sort)`
Space `O(sort)`
Sliding window
Time `O(n)`
Space `O(1)`
<br>
```Java [Java]
public long maximumCoins(int[][] A, int k) {
Arrays.sort(A, (a, b) -> a[0] - b[0]);
int n = A.length;
// Start at A[i][0]
long res = 0, cur = 0;
for (int i = 0, j = 0; i < n; ++i) {
while (j < n && A[j][1] <= A[i][0] + k - 1) {
cur += 1L * (A[j][1] - A[j][0] + 1) * A[j][2];
j++;
}
if (j < n) {
long part = 1L * Math.max(0, A[i][0] + k - 1 - A[j][0] + 1) * A[j][2];
res = Math.max(res, cur + part);
}
cur -= 1L * (A[i][1] - A[i][0] + 1) * A[i][2];
}
// End at A[i][1]
cur = 0;
for (int i = 0, j = 0; i < n; ++i) {
cur += 1L * (A[i][1] - A[i][0] + 1) * A[i][2];
while (A[j][1] < A[i][1] - k + 1) {
cur -= 1L * (A[j][1] - A[j][0] + 1) * A[j][2];
j++;
}
long part = 1L * Math.max(0, A[i][1] - k - A[j][0] + 1) * A[j][2];
res = Math.max(res, cur - part);
}
return res;
}
```
```C++ [C++]
long long maximumCoins(vector<vector<int>>& A, int k) {
sort(A.begin(), A.end());
int n = A.size();
// Start at A[i][0]
long long res = 0, cur = 0;
for (int i = 0, j = 0; i < n; ++i) {
while (j < n && A[j][1] <= A[i][0] + k - 1) {
cur += 1L * (A[j][1] - A[j][0] + 1) * A[j][2];
j++;
}
if (j < n) {
long long part = 1L * max(0, A[i][0] + k - 1 - A[j][0] + 1) * A[j][2];
res = max(res, cur + part);
}
cur -= 1L * (A[i][1] - A[i][0] + 1) * A[i][2];
}
// End at A[i][1]
cur = 0;
for (int i = 0, j = 0; i < n; ++i) {
cur += 1L * (A[i][1] - A[i][0] + 1) * A[i][2];
while (A[j][1] < A[i][1] - k + 1) {
cur -= 1L * (A[j][1] - A[j][0] + 1) * A[j][2];
j++;
}
long long part = 1L * max(0, A[i][1] - k - A[j][0] + 1) * A[j][2];
res = max(res, cur - part);
}
return res;
}
```
```py [Python3]
def maximumCoins(self, A: List[List[int]], k: int) -> int:
def slide(A):
A.sort()
res = cur = j = 0
for i in range(len(A)):
cur += (A[i][1] - A[i][0] + 1) * A[i][2]
while A[j][1] < A[i][1] - k + 1:
cur -= (A[j][1] - A[j][0] + 1) * A[j][2]
j += 1
part = max(0, A[i][1] - k - A[j][0] + 1) * A[j][2]
res = max(res, cur - part)
return res
return max(slide(A), slide([[-r,-l,w] for l,r,w in A]))
```
# More Similar Sliding Window Problems
Here are some similar sliding window problems.
Also find more explanations and discussion.
Good luck and have fun.
- [3413. Maximum Coins From K Consecutive Bags](https://leetcode.com/problems/maximum-coins-from-k-consecutive-bags/solutions/6232195/javacpython-sliding-window-by-lee215-3418/)
- [3347. Maximum Frequency of an Element After Performing Operations II](https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-ii/discuss/6027259/JavaC%2B%2BPython-Two-cases)
- [2958. Length of Longest Subarray With at Most K Frequency](https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/solutions/4382698/java-c-python-sliding-window/)
- [2831. Find the Longest Equal Subarray](https://leetcode.com/problems/find-the-longest-equal-subarray/discuss/3934172/JavaC%2B%2BPython-One-Pass-Sliding-Window-O(n))
- [2799. Count Complete Subarrays in an Array](https://leetcode.com/problems/count-complete-subarrays-in-an-array/discuss/3836137/JavaC%2B%2BPython-Sliding-Window-O(n))
- [2779. Maximum Beauty of an Array After Applying Operation](https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/discuss/3771308/JavaC%2B%2BPython-Sliding-Window)
- [2730. Find the Longest Semi-Repetitive Substring](https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/discuss/3629621/JavaC%2B%2BPython-Sliding-Window)
- [2555. Maximize Win From Two Segments](https://leetcode.com/problems/maximize-win-from-two-segments/discuss/3141449/JavaC%2B%2BPython-DP-%2B-Sliding-Segment-O(n))
- [2537. Count the Number of Good Subarrays](https://leetcode.com/problems/count-the-number-of-good-subarrays/discuss/3052559/C%2B%2BPython-Sliding-Window)
- [2401. Longest Nice Subarray](https://leetcode.com/problems/longest-nice-subarray/discuss/2527496/Python-Sliding-Window)
- [2398. Maximum Number of Robots Within Budget](https://leetcode.com/problems/maximum-number-of-robots-within-budget/discuss/2524838/Python-Sliding-Window-O(n))
- [2024. Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/discuss/1499049/JavaC%2B%2BPython-Sliding-Window-strict-O(n))
- [1838. Frequency of the Most Frequent Element](https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1175090/JavaC%2B%2BPython-Sliding-Window)
- [1493. Longest Subarray of 1's After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/discuss/708112/JavaC%2B%2BPython-Sliding-Window-at-most-one-0)
- [1425. Constrained Subsequence Sum](https://leetcode.com/problems/constrained-subsequence-sum/discuss/597751/JavaC++Python-O(N)-Decreasing-Deque)
- [1358. Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/discuss/516977/JavaC++Python-Easy-and-Concise)
- [1248. Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/discuss/419378/JavaC%2B%2BPython-Sliding-Window-atMost(K)-atMost(K-1))
- [1234. Replace the Substring for Balanced String](https://leetcode.com/problems/replace-the-substring-for-balanced-string/discuss/408978/javacpython-sliding-window/)
- [1004. Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/discuss/247564/JavaC%2B%2BPython-Sliding-Window)
- [930. Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/discuss/186683/)
- [992. Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/523136/JavaC%2B%2BPython-Sliding-Window)
- [904. Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/discuss/170740/Sliding-Window-for-K-Elements)
- [862. Shortest Subarray with Sum at Least K](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/discuss/143726/C%2B%2BJavaPython-O(N)-Using-Deque)
- [424. Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/discuss/278271/JavaC%2B%2BPython-Sliding-Window-just-O(n))
- [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/discuss/433123/JavaC++Python-Sliding-Window)
| 91 | 2 | ['C++', 'Java', 'Python3'] | 13 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.