3000. Maximum Area of Longest Diagonal Rectangle LeetCode Solution
In this guide, you will get 3000. Maximum Area of Longest Diagonal Rectangle LeetCode Solution with the best time and space complexity. The solution to Maximum Area of Longest Diagonal Rectangle problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Area of Longest Diagonal Rectangle solution in C++
- Maximum Area of Longest Diagonal Rectangle solution in Java
- Maximum Area of Longest Diagonal Rectangle solution in Python
- Additional Resources
Problem Statement of Maximum Area of Longest Diagonal Rectangle
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. Diagonal length = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈ 9.487.
For index = 1, length = 8 and width = 6. Diagonal length = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10.
So, the rectangle at index 1 has a greater diagonal length therefore we return area = 8 * 6 = 48.
Example 2:
Input: dimensions = [[3,4],[4,3]]
Output: 12
Explanation: Length of diagonal is the same for both which is 5, so maximum area = 12.
Constraints:
1 <= dimensions.length <= 100
dimensions[i].length == 2
1 <= dimensions[i][0], dimensions[i][1] <= 100
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
3000. Maximum Area of Longest Diagonal Rectangle LeetCode Solution in C++
class Solution {
public:
int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
const vector<int> maxDimension = *ranges::max_element(
dimensions, [](const vector<int>& a, const vector<int>& b) {
return (a[0] * a[0] + a[1] * a[1] == b[0] * b[0] + b[1] * b[1])
? (a[0] * a[1] < b[0] * b[1])
: (a[0] * a[0] + a[1] * a[1] < b[0] * b[0] + b[1] * b[1]);
});
return maxDimension[0] * maxDimension[1];
}
};
/* code provided by PROGIEZ */
3000. Maximum Area of Longest Diagonal Rectangle LeetCode Solution in Java
class Solution {
public int areaOfMaxDiagonal(int[][] dimensions) {
int[] maxDimension =
Arrays.stream(dimensions)
.max((a, b)
-> a[0] * a[0] + a[1] * a[1] == b[0] * b[0] + b[1] * b[1]
? Integer.compare(a[0] * a[1], b[0] * b[1])
: Integer.compare(a[0] * a[0] + a[1] * a[1], b[0] * b[0] + b[1] * b[1]))
.orElseThrow();
return maxDimension[0] * maxDimension[1];
}
}
// code provided by PROGIEZ
3000. Maximum Area of Longest Diagonal Rectangle LeetCode Solution in Python
class Solution:
def areaOfMaxDiagonal(self, dimensions: list[list[int]]) -> int:
a, b = max(dimensions, key=lambda x: (x[0]**2 + x[1]**2, x[0] * x[1]))
return a * b
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.