Skip to content

Commit e092e54

Browse files
committed
1779. Find Nearest Point That Has the Same X or Y Coordinate: AC
1 parent c694ad5 commit e092e54

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,3 +1340,4 @@ mod s1773_count_items_matching_a_rule;
13401340
mod s1774_closest_dessert_cost;
13411341
mod s1775_equal_sum_arrays_with_minimum_number_of_operations;
13421342
mod s1776_car_fleet_ii;
1343+
mod s1779_find_nearest_point_that_has_the_same_x_or_y_coordinate;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* [1779] Find Nearest Point That Has the Same X or Y Coordinate
3+
*
4+
* You are given two integers, x and y, which represent your current location on a Cartesian grid: (x, y). You are also given an array points where each points[i] = [ai, bi] represents that a point exists at (ai, bi). A point is valid if it shares the same x-coordinate or the same y-coordinate as your location.
5+
* Return the index (0-indexed) of the valid point with the smallest Manhattan distance from your current location. If there are multiple, return the valid point with the smallest index. If there are no valid points, return -1.
6+
* The Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2).
7+
*
8+
* Example 1:
9+
*
10+
* Input: x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
11+
* Output: 2
12+
* Explanation: Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.
13+
* Example 2:
14+
*
15+
* Input: x = 3, y = 4, points = [[3,4]]
16+
* Output: 0
17+
* Explanation: The answer is allowed to be on the same location as your current location.
18+
* Example 3:
19+
*
20+
* Input: x = 3, y = 4, points = [[2,3]]
21+
* Output: -1
22+
* Explanation: There are no valid points.
23+
*
24+
* Constraints:
25+
*
26+
* 1 <= points.length <= 10^4
27+
* points[i].length == 2
28+
* 1 <= x, y, ai, bi <= 10^4
29+
*
30+
*/
31+
pub struct Solution {}
32+
33+
// problem: https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/
34+
// discuss: https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/discuss/?currentPage=1&orderBy=most_votes&query=
35+
36+
// submission codes start here
37+
38+
impl Solution {
39+
pub fn nearest_valid_point(x: i32, y: i32, points: Vec<Vec<i32>>) -> i32 {
40+
points
41+
.into_iter()
42+
.enumerate()
43+
.filter(|(_, point)| point[0] == x || point[1] == y)
44+
.min_by_key(|(_, point)| x.abs_diff(point[0]) + y.abs_diff(point[1]))
45+
.map_or(-1, |(i, _)| i as i32)
46+
}
47+
}
48+
49+
// submission codes end
50+
51+
#[cfg(test)]
52+
mod tests {
53+
use super::*;
54+
55+
#[test]
56+
fn test_1779_example_1() {
57+
let x = 3;
58+
let y = 4;
59+
let points = vec![vec![1, 2], vec![3, 1], vec![2, 4], vec![2, 3], vec![4, 4]];
60+
61+
let result = 2;
62+
63+
assert_eq!(Solution::nearest_valid_point(x, y, points), result);
64+
}
65+
66+
#[test]
67+
fn test_1779_example_2() {
68+
let x = 3;
69+
let y = 4;
70+
let points = vec![vec![3, 4]];
71+
72+
let result = 0;
73+
74+
assert_eq!(Solution::nearest_valid_point(x, y, points), result);
75+
}
76+
77+
#[test]
78+
fn test_1779_example_3() {
79+
let x = 3;
80+
let y = 4;
81+
let points = vec![vec![2, 3]];
82+
83+
let result = -1;
84+
85+
assert_eq!(Solution::nearest_valid_point(x, y, points), result);
86+
}
87+
}

0 commit comments

Comments
 (0)