LeetCode 3000- Maximum Area of Longest Diagonal Rectangle. use c++
视频信息
答案文本
视频字幕
Welcome to LeetCode problem 3000: Maximum Area of Longest Diagonal Rectangle. In this problem, we're given a 2D array where each element represents a rectangle with length and width. Our goal is to find the rectangle with the maximum area among those that have the longest diagonal. For example, given rectangles [9,3], [8,6], and [4,2], we calculate their diagonals using the Pythagorean theorem. Rectangle [8,6] has the longest diagonal of 10.0 and area 48, making it our answer.
To solve this problem, we need to understand two key mathematical concepts. First, the diagonal length of a rectangle is calculated using the Pythagorean theorem: d equals the square root of length squared plus width squared. Second, the area is simply length times width. For example, with a rectangle of length 6 and width 4, the diagonal is the square root of 36 plus 16, which equals approximately 7.21, and the area is 24. Our selection criteria is to first find all rectangles with the longest diagonal, then among those, choose the one with maximum area.
Now let's develop our algorithm strategy. We'll use a single-pass approach with two tracking variables: maxDiagonal to store the longest diagonal found so far, and maxArea to store the maximum area among rectangles with that longest diagonal. For each rectangle, we calculate its diagonal length. If this diagonal is greater than our current maximum, we update both maxDiagonal and reset maxArea to the current rectangle's area. If the diagonal equals our current maximum, we only update maxArea if the current area is larger. This ensures we find the rectangle with maximum area among those with the longest diagonal.
Today we'll solve LeetCode problem 3000: Maximum Area of Longest Diagonal Rectangle. Given a 2D array of rectangle dimensions, we need to find the area of the rectangle with the longest diagonal. If multiple rectangles have the same longest diagonal, we return the one with maximum area.
To understand this problem, let's look at rectangle diagonals. For any rectangle with length L and width W, the diagonal length is calculated using the Pythagorean theorem: square root of L squared plus W squared. The area is simply L times W. Our goal is to find the rectangle with the longest diagonal, and if multiple rectangles have the same diagonal length, we choose the one with maximum area.
Our algorithm follows a straightforward approach. First, we initialize two tracking variables: maxDiagonal to store the longest diagonal found so far, and maxArea to store the area of the rectangle with that longest diagonal. Then we iterate through each rectangle, calculating its diagonal using the Pythagorean theorem and its area. Our update logic is key: if the current diagonal is longer than our maximum, we update both variables. If the diagonal equals our current maximum, we only update the area if it's larger than what we've seen. Finally, we return the maximum area.
Here's our C++ implementation. We include the necessary headers: vector for input handling, cmath for mathematical functions, and algorithm for the max function. The solution class contains our main function that takes a 2D vector of dimensions. We initialize maxDiagonal as double and maxArea as integer. In our loop, we extract length and width, calculate the diagonal using sqrt and pow functions, and compute the area. Our conditional logic handles both cases: when we find a longer diagonal, and when we find equal diagonal but larger area. The algorithm runs in O(n) time with O(1) space complexity.
Let's walk through an example. Given dimensions [[9,3],[8,6],[4,8]], we calculate each rectangle's diagonal and area. Rectangle 1 with dimensions 9×3 has diagonal approximately 9.49 and area 27. Rectangle 2 with dimensions 8×6 has diagonal exactly 10 and area 48. Rectangle 3 with dimensions 4×8 has diagonal approximately 8.94 and area 32. Since rectangle 2 has the longest diagonal of 10, we return its area of 48. This demonstrates how our algorithm efficiently finds the correct answer by comparing diagonal lengths first, then areas when needed.
Let's trace through a detailed example with input [[9,3],[8,6],[4,2],[7,4]]. We start with rectangle [9,3]: diagonal equals square root of 90, approximately 9.49, and area equals 27. We initialize both tracking variables. Next, rectangle [8,6] has diagonal 10.0 and area 48. Since 10.0 is greater than 9.49, we update both maxDiagonal to 10.0 and maxArea to 48. Rectangle [4,2] has diagonal 4.47 and area 8, but since 4.47 is less than 10.0, no update occurs. Finally, rectangle [7,4] has diagonal 8.06 and area 28, again less than 10.0, so no update. The final result is 48, from rectangle [8,6] which had the longest diagonal.