Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Find Building Where Alice and Bob Can Meet

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2940" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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 104 1 <= heights[i] <= 109 1 <= queries.length <= 5 104 queries[i] = [ai, bi] 0 <= ai, bi <= heights.length - 1

Explanation

Here's a breakdown of the solution:

  • Iterate through queries: Process each query independently to find the leftmost meeting point.
  • Linear scan for each query: For each query (a, b), iterate through potential meeting points 'j' (starting from max(a,b)). Check if both Alice (from 'a') and Bob (from 'b') can reach 'j'. If so, return j immediately as it's the leftmost.
  • Early exit optimization: if a == b, return a immediately since they are already in the same building.

  • Runtime Complexity: O(Q * N), where Q is the number of queries and N is the length of the heights array.

  • Storage Complexity: O(Q), where Q is the number of queries (to store the ans array).
def solve():
    def leftmost_meeting_building(heights, queries):
        n = len(heights)
        ans = []

        def can_reach(start, end, heights):
            return start < end and heights[start] < heights[end]

        for a, b in queries:
            if a == b:
                ans.append(a)
                continue

            meeting_point = -1
            for j in range(max(a, b), n):
                alice_can_reach = can_reach(a, j, heights)
                bob_can_reach = can_reach(b, j, heights)

                if alice_can_reach and bob_can_reach:
                    meeting_point = j
                    break
                elif a == j and can_reach(b, j, heights):
                     meeting_point = j
                     break
                elif b == j and can_reach(a, j, heights):
                     meeting_point = j
                     break


            ans.append(meeting_point)
        return ans

    heights = [6,4,8,5,2,7]
    queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]
    print(leftmost_meeting_building(heights, queries))

    heights = [5,3,8,2,6,1,4,6]
    queries = [[0,7],[3,5],[5,2],[3,0],[1,6]]
    print(leftmost_meeting_building(heights, queries))


def leftmost_building_where_alice_and_bob_can_meet(heights, queries):
    n = len(heights)
    ans = []

    def can_reach(start, end, heights):
        return start < end and heights[start] < heights[end]

    for a, b in queries:
        if a == b:
            ans.append(a)
            continue

        meeting_point = -1
        for j in range(max(a, b), n):
            alice_can_reach = can_reach(a, j, heights)
            bob_can_reach = can_reach(b, j, heights)

            if alice_can_reach and bob_can_reach:
                meeting_point = j
                break
            elif a == j and can_reach(b, j, heights):
                 meeting_point = j
                 break
            elif b == j and can_reach(a, j, heights):
                 meeting_point = j
                 break


        ans.append(meeting_point)
    return ans


    # Code
    ```python
    def leftmost_building_where_alie_and_bob_an_meet(heights, queries):    n = len(heights)
    ans = []

    def can_reach(start, end, heights):
        return start < end and heights[start] < heights[end]

    for a, b in queries:
        if a == b:
            ans.append(a)
            continue

        meeting_point = -1
        for j in range(max(a, b), n):
            alice_can_reach = can_reach(a, j, heights)
            bob_can_reach = can_reach(b, j, heights)

            if alice_can_reach and bob_can_reach:
                meeting_point = j
                break
            elif a == j and can_reach(b, j, heights):
                 meeting_point = j
                 break
            elif b == j and can_reach(a, j, heights):
                 meeting_point = j
                 break


        ans.append(meeting_point)
    return ans

More from this blog

C

Chatmagic blog

2894 posts