✅THE BEST EXPLANATION||Beats 100% Users
---\n# PLEASE UPVOTE IF YOU LIKE IT \u2705\n---\n\n\n\n---\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to find the r
---\n# PLEASE UPVOTE IF YOU LIKE IT \u2705\n---\n![Screenshot (3221).png](https://assets.leetcode.com/users/images/6536f4b4-7f48-4afd-a9cd-af2809956ddc_1704612700.2210727.png)\n\n\n---\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to find the rectangle with the longest diagonal. If there are multiple rectangles with the longest diagonal, the tiebreaker is the one with the maximum area.\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize variables maxArea and maxDiag to keep track of the maximum area and diagonal encountered so far.\n2. Iterate through each rectangle represented by dimensions[i].\n3. For each rectangle, calculate the square of the diagonal length using the formula (l * l + w * w), where l is the length and w is the width. **Note that there is no need to calculate the square root, as comparing squared values maintains the order of the lengths.**\n4. Compare the calculated diagonal length with the current maximum diagonal (maxDiag).\n5. If the current diagonal is greater than the maximum diagonal encountered so far, or if it\'s equal but the area of the rectangle is greater, update maxDiag and maxArea accordingly.\n6. Continue this process for all rectangles.\n7. Return the final maximum area (maxArea) as the result.\n\n---\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe **time complexity of the provided code is O(n)**, where n is the number of rectangles represented by dimensions. In the worst case, the code iterates through all the rectangles once, performing constant-time operations for each rectangle. Therefore, the overall time complexity is linear with respect to the number of rectangles.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe **space complexity is O(1)**, constant space. The code uses only a constant amount of additional space regardless of the input size. The space required for variables maxArea, maxDiag, and the loop variables does not depend on the number of rectangles. Hence, the space complexity is constant.\n\n---\n\n![download.jpeg](https://assets.leetcode.com/users/images/e76b43cf-dafc-4a48-9a68-6f60d3020fb6_1704346279.413626.jpeg)\n\n---\n# Code\n\n---\n\n\n## **C++**\n```\nclass Solution {\npublic:\n    // Function to calculate the area of the rectangle with the longest diagonal\n    int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {\n        // Get the number of rectangles\n        int n = dimensions.size();\n\n        // Initialize variables to keep track of maximum area and diagonal\n        int maxArea = 0, maxDiag = 0;\n\n        // Iterate through each rectangle\n        for (int i = 0; i < n; i++) {\n            // Get the length and width of the current rectangle\n            int l = dimensions[i][0];\n            int w = dimensions[i][1];\n\n            // Calculate the square of the diagonal length\n            int currDiag = l * l + w * w;\n\n            // Compare the current diagonal with the maximum diagonal encountered so far\n            // If the current diagonal is greater, or if it\'s equal but the area is greater,\n            // update maxDiag and maxArea accordingly\n            if (currDiag > maxDiag || (currDiag == maxDiag && l * w > maxArea)) {\n                maxDiag = currDiag;\n                maxArea = l * w;\n            }\n        }\n\n        // Return the maximum area of the rectangle with the longest diagonal\n        return maxArea;\n    }\n};\n```\n\n---\n\n\n### **Python3**\n```\nclass Solution:\n    def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:\n        # Get the number of rectangles\n        n = len(dimensions)\n\n        # Initialize variables to keep track of maximum area and diagonal\n        maxArea = 0\n        maxDiag = 0\n\n        # Iterate through each rectangle\n        for i in range(n):\n            # Get the length and width of the current rectangle\n            l, w = dimensions[i]\n\n            # Calculate the square of the diagonal length\n            currDiag = l**2 + w**2\n\n            # Compare the current diagonal with the maximum diagonal encountered so far\n            # If the current diagonal is greater, or if it\'s equal but the area is greater,\n            # update maxDiag and maxArea accordingly\n            if currDiag > maxDiag or (currDiag == maxDiag and l * w > maxArea):\n                maxDiag = currDiag\n                maxArea = l * w\n\n        # Return the maximum area of the rectangle with the longest diagonal\n        return maxArea\n\n```\n\n---\n\n\n### **Java**\n```\nclass Solution {\n    public int areaOfMaxDiagonal(int[][] dimensions) {\n        // Get the number of rectangles\n        int n = dimensions.length;\n\n        // Initialize variables to keep track of maximum area and diagonal\n        int maxArea = 0;\n        int maxDiag = 0;\n\n        // Iterate through each rectangle\n        for (int i = 0; i < n; i++) {\n            // Get the length and width of the current rectangle\n            int l = dimensions[i][0];\n            int w = dimensions[i][1];\n\n            // Calculate the square of the diagonal length\n            int currDiag = l * l + w * w;\n\n            // Compare the current diagonal with the maximum diagonal encountered so far\n            // If the current diagonal is greater, or if it\'s equal but the area is greater,\n            // update maxDiag and maxArea accordingly\n            if (currDiag > maxDiag || (currDiag == maxDiag && l * w > maxArea)) {\n                maxDiag = currDiag;\n                maxArea = l * w;\n            }\n        }\n\n        // Return the maximum area of the rectangle with the longest diagonal\n        return maxArea;\n    }\n}\n```\n\n---\n\n\n### **JavaScript**\n```\n/**\n * @param {number[][]} dimensions\n * @return {number}\n */\nvar areaOfMaxDiagonal = function(dimensions) {\n    // Get the number of rectangles\n    const n = dimensions.length;\n\n    // Initialize variables to keep track of maximum area and diagonal\n    let maxArea = 0;\n    let maxDiag = 0;\n\n    // Iterate through each rectangle\n    for (let i = 0; i < n; i++) {\n        // Get the length and width of the current rectangle\n        const l = dimensions[i][0];\n        const w = dimensions[i][1];\n\n        // Calculate the square of the diagonal length\n        const currDiag = l ** 2 + w ** 2;\n\n        // Compare the current diagonal with the maximum diagonal encountered so far\n        // If the current diagonal is greater, or if it\'s equal but the area is greater,\n        // update maxDiag and maxArea accordingly\n        if (currDiag > maxDiag || (currDiag === maxDiag && l * w > maxArea)) {\n            maxDiag = currDiag;\n            maxArea = l * w;\n        }\n    }\n\n    // Return the maximum area of the rectangle with the longest diagonal\n    return maxArea;\n};\n```\n\n---\n\n
['Python', 'C++', 'Java', 'Python3', 'JavaScript']