Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Building Height

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1840" 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 want to build n new buildings in a city. The new buildings will be built in a line and are labeled from 1 to n. However, there are city restrictions on the heights of the new buildings: The height of each building must be a non-negative integer. The height of the first building must be 0. The height difference between any two adjacent buildings cannot exceed 1. Additionally, there are city restrictions on the maximum height of specific buildings. These restrictions are given as a 2D integer array restrictions where restrictions[i] = [idi, maxHeighti] indicates that building idi must have a height less than or equal to maxHeighti. It is guaranteed that each building will appear at most once in restrictions, and building 1 will not be in restrictions. Return the maximum possible height of the tallest building. Example 1: Input: n = 5, restrictions = [[2,1],[4,1]] Output: 2 Explanation: The green area in the image indicates the maximum allowed height for each building. We can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 2. Example 2: Input: n = 6, restrictions = [] Output: 5 Explanation: The green area in the image indicates the maximum allowed height for each building. We can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5. Example 3: Input: n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]] Output: 5 Explanation: The green area in the image indicates the maximum allowed height for each building. We can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a height of 5. Constraints: 2 <= n <= 109 0 <= restrictions.length <= min(n - 1, 105) 2 <= idi <= n idi is unique. 0 <= maxHeighti <= 109

Explanation

Here's a breakdown of the approach and the Python code:

  • Key Idea: The problem can be solved by iterating through the restrictions and calculating the maximum possible height between adjacent restrictions (or between the start and the first restriction, and the last restriction and the end). The height between any two restrictions is limited by their heights and distance.
  • Optimization: Sort the restrictions by building index. This allows us to iterate through them in order and efficiently calculate the maximum possible height between them.
  • Maximization: The code calculates the maximum possible height between each pair of restrictions and updates the overall maximum height found so far.

  • Complexity: O(N log N) runtime, O(N) storage, where N is the number of restrictions. The O(N log N) comes from the sorting operation.

Code

    def max_building(n: int, restrictions: list[list[int]]) -> int:
    """
    Calculates the maximum possible height of the tallest building given restrictions.

    Args:
        n: The total number of buildings.
        restrictions: A list of restrictions, where each restriction is a list [id, maxHeight].

    Returns:
        The maximum possible height of the tallest building.
    """

    restrictions.sort()
    restrictions = [[1, 0]] + restrictions

    # Enforce height restrictions from left to right
    for i in range(1, len(restrictions)):
        dist = restrictions[i][0] - restrictions[i - 1][0]
        restrictions[i][1] = min(restrictions[i][1], restrictions[i - 1][1] + dist)

    # Enforce height restrictions from right to left
    for i in range(len(restrictions) - 2, -1, -1):
        dist = restrictions[i + 1][0] - restrictions[i][0]
        restrictions[i][1] = min(restrictions[i][1], restrictions[i + 1][1] + dist)

    max_height = 0
    for i in range(1, len(restrictions)):
        dist = restrictions[i][0] - restrictions[i - 1][0]
        h1 = restrictions[i - 1][1]
        h2 = restrictions[i][1]

        # Calculate the height of the tallest building between the two restrictions
        height_between = max(h1, h2) + (dist - abs(h1 - h2)) // 2
        max_height = max(max_height, height_between)

    #Consider the last building
    max_height = max(max_height, restrictions[-1][1] + (n - restrictions[-1][0]))


    return max_height

More from this blog

C

Chatmagic blog

2894 posts