You are given a 0-indexed array heights of positive integers, where heights[i] represents the height of the ith building.
If a person is in building i, they can move to any other building j if and only if i < j and heights[i] < heights[j].
You are also given another array queries where queries[i] = [ai, bi]. On the ith query, Alice is in building ai while Bob is in building bi.
Return an array ans where ans[i] is the index of the leftmost building where Alice and Bob can meet on the ith query. If Alice and Bob cannot move to a common building on query i, set ans[i] to -1.
Example 1:
Input: heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]] Output: [2,5,-1,5,2] Explanation: In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2]. In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5]. In the third query, Alice cannot meet Bob since Alice cannot move to any other building. In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5]. In the fifth query, Alice and Bob are already in the same building. For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Example 2:
Input: heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]] Output: [7,6,-1,4,6] Explanation: In the first query, Alice can directly move to Bob's building since heights[0] < heights[7]. In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6]. In the third query, Alice cannot meet Bob since Bob cannot move to any other building. In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4]. In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6]. For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Constraints:
1 <= heights.length <= 5 * 1041 <= heights[i] <= 1091 <= queries.length <= 5 * 104queries[i] = [ai, bi]0 <= ai, bi <= heights.length - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves trying every possible meeting location for Alice and Bob. We simply go through each building and check if it's a valid meeting point considering their travel constraints. This ensures we don't miss any potential solutions.
Here's how the algorithm would work step-by-step:
def find_meeting_building(alice_travel_times, bob_travel_times, max_alice_travel_time, max_bob_travel_time):
possible_meeting_buildings = []
number_of_buildings = len(alice_travel_times)
for building_index in range(number_of_buildings):
# Check if Alice can reach this building within her time
if alice_travel_times[building_index] <= max_alice_travel_time:
# Check if Bob can reach this building within his time
if bob_travel_times[building_index] <= max_bob_travel_time:
# We need to remember this possible location
possible_meeting_buildings.append(building_index)
return possible_meeting_buildingsThe problem asks us to find a building where both Alice and Bob can see each other. The buildings are arranged in a line, and they can only see each other if there are no taller buildings in between. We can solve this efficiently by checking buildings from both ends of the line towards the middle.
Here's how the algorithm would work step-by-step:
def find_meeting_building(building_heights):
number_of_buildings = len(building_heights)
alice_visible_buildings = []
bob_visible_buildings = []
tallest_alice_can_see = -1
tallest_bob_can_see = -1
# Iterate from left to right to find Alice's visible buildings.
for i in range(number_of_buildings):
if building_heights[i] > tallest_alice_can_see:
tallest_alice_can_see = building_heights[i]
alice_visible_buildings.append(i)
# Iterate from right to left to find Bob's visible buildings.
for i in range(number_of_buildings - 1, -1, -1):
if building_heights[i] > tallest_bob_can_see:
tallest_bob_can_see = building_heights[i]
bob_visible_buildings.append(i)
bob_visible_buildings.reverse()
# Find the first common building in both lists.
for alice_building in alice_visible_buildings:
for bob_building in bob_visible_buildings:
if alice_building == bob_building:
#Return the index if they can both see the building.
return alice_building
# If no common building is found, return -1.
return -1| Case | How to Handle |
|---|---|
| Null or empty heights array | Return -1 immediately since no valid building can be found. |
| Heights array with a single element | Check if the single element's height is within Alice and Bob's jump heights; if so, return 0, otherwise -1. |
| Alice and Bob cannot reach any buildings (their jump heights are too small) | The solution should return -1 as no building will be marked as reachable by both. |
| The first and last building are too tall for Alice or Bob respectively | The initial reachable sets for Alice and Bob will be empty, resulting in no meeting point and returning -1. |
| Only one building satisfies the height constraint for both Alice and Bob | The solution should return the index of that single building if both can reach it. |
| Large input array leading to potential performance issues | Use an iterative approach with O(n) time complexity to efficiently determine reachable buildings for Alice and Bob. |
| Integer overflow if heights are very large | Since we are only comparing heights, overflow is not a concern, but language-specific integer limits could be if indexes become too large, handle large index values. |
| Multiple buildings satisfy the meeting criteria, requiring selection of the closest to building 0 | The algorithm should prioritize checking buildings from index 0 onwards, automatically selecting the closest building when it finds a valid meeting point. |