Loading...
Loading...
1 <= hotels.length <= 10^7 1 <= k <= hotels.length -10^6 <= x, y <= 10^6 All hotel ids are unique integers Coordinates are floating point values
You are building a hotel recommendation feature for a travel app. Given a user's current GPS coordinate and a list of hotels (each with their own GPS coordinates), return the K closest hotels to the user.
Distance is measured using the standard Euclidean distance formula:
distance = sqrt((x2 - x1)² + (y2 - y1)²)
You do not need to return the results sorted by distance — any valid set of K closest hotels is acceptable.
hotels: A list of [id, x, y] where id is a unique integer hotel identifier, and x, y are GPS coordinates (floats).target: A list [x, y] representing the user's current GPS location.k: An integer representing the number of closest hotels to return.Return a list of hotel ids (integers) representing the K closest hotels. The order does not matter.
Input:
hotels = [[1, 1.0, 2.0], [2, 5.0, 4.0], [3, 3.0, 3.0], [4, -1.0, -1.0]]
target = [0.0, 0.0]
k = 2
Output: [1, 4]
Explanation: Distances from (0, 0):
The 2 closest are Hotel 4 (≈1.41) and Hotel 1 (≈2.24).
Input:
hotels = [[10, 0.0, 1.0], [20, 0.0, -1.0], [30, 1.0, 0.0]]
target = [0.0, 0.0]
k = 1
Output: [10] or [20] or [30] (any one is valid)
Explanation: All hotels are at distance 1.0. Any single hotel is a valid answer.
Input:
hotels = [[1, 2.0, 3.0]]
target = [5.0, 7.0]
k = 1
Output: [1]
Explanation: Only one hotel exists, so it must be returned.