Advertisement

Maximum Area of Longest Diagonal Rectangle - LeetCode 3000 Solution

Maximum Area of Longest Diagonal Rectangle - Complete Solution Guide

Maximum Area of Longest Diagonal Rectangle is LeetCode problem 3000, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a 2D 0-indexed integer array dimensions . For all indices i , 0 <= i < dimensions.length , dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i . Return the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area. Example 1: Input: dimensions = [[9,3],[8,6]] Output: 48 Explanation: For index = 0, length = 9 and width = 3. Dia

Detailed Explanation

The problem asks you to find the area of the rectangle with the longest diagonal from a given list of rectangles. Each rectangle is represented by its length and width in a 2D array. If multiple rectangles have the same longest diagonal, the one with the largest area is selected. The input is a 2D integer array `dimensions` where each inner array `[length, width]` represents a rectangle. The output is a single integer representing the area of the selected rectangle.

Solution Approach

The solution iterates through each rectangle in the `dimensions` array. For each rectangle, it calculates the diagonal length and the area. It keeps track of the maximum diagonal length encountered so far and the area of the rectangle with that diagonal. If a rectangle has a longer diagonal, it updates the maximum diagonal and its corresponding area. If a rectangle has the same maximum diagonal, it compares its area with the current maximum area and updates the maximum area if necessary.

Step-by-Step Algorithm

  1. Initialize `maxDiagonal` to 0 and `maxArea` to 0.
  2. Iterate through each rectangle in the `dimensions` array.
  3. Calculate the diagonal length using the Pythagorean theorem: `diagonal = sqrt(length^2 + width^2)`.
  4. Calculate the area: `area = length * width`.
  5. If `diagonal > maxDiagonal`, update `maxDiagonal` to `diagonal` and `maxArea` to `area`.
  6. If `diagonal == maxDiagonal`, update `maxArea` to `max(maxArea, area)`.
  7. After iterating through all rectangles, return `maxArea`.

Key Insights

  • The problem requires calculating the diagonal length of each rectangle using the Pythagorean theorem (diagonal = sqrt(length^2 + width^2)).
  • A straightforward iterative approach is sufficient to find the maximum diagonal and corresponding area. No sophisticated data structures are needed.
  • Special handling is required to manage cases where multiple rectangles share the same maximum diagonal length; in this case, we need to choose the one with the maximum area.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array.

Companies

Asked at: Accenture, Atlassian.