100% faster | easy to understand | readable | optimal | java | cpp | python
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves finding the largest area of a square that can be formed by interse
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves finding the largest area of a square that can be formed by intersecting two rectangles among given sets of rectangles. Essentially, we need to identify pairs of rectangles that intersect and determine the maximum square area within their intersection.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Iterative Comparison: The solution iterates over each pair of rectangles to check for intersections.\n- Intersection Check: For each pair of rectangles, it checks whether they intersect. If they do, it calculates the dimensions of the intersection rectangle.\n- Maximum Square Area: Within the intersection rectangle, the solution calculates the maximum square area possible by taking the minimum of the width and height of the intersection.\n- Updating Maximum Area: It updates the maximum square area encountered so far.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\no(n^2);\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\no(1)\n# Code\n```java []\nclass Solution {\n    public long largestSquareArea(int[][] bottomLeft, int[][] topRight) {\n        int ans = 0, n = bottomLeft.length;\n        for (int i = 0; i < n; i++) {\n            int[] firstRectBL = bottomLeft[i];\n            int[] firstRectTR = topRight[i];\n            for (int j = i + 1; j < n; j++) {\n                int[] secondRectBL = bottomLeft[j];\n                int[] secondRectTR = topRight[j];\n                if (secondRectBL[0] >= firstRectTR[0] || secondRectTR[0] <= firstRectBL[0]) continue;\n                if (secondRectTR[1] <= firstRectBL[1] || secondRectBL[1] >= firstRectTR[1]) continue;\n                int pntAx = Math.max(firstRectBL[0], secondRectBL[0]);\n                int pntAy = Math.max(firstRectBL[1], secondRectBL[1]);\n                int pntBx = Math.min(firstRectTR[0], secondRectTR[0]);\n                int pntBy = Math.min(firstRectTR[1], secondRectTR[1]);\n                int sideA = pntBx - pntAx;\n                int sideB = pntBy - pntAy;\n                ans = Math.max(ans, Math.min(sideA, sideB));\n            }\n        }\n        return (long) ans * ans;\n    }\n}\n\n\n```\n```cpp []\n\nclass Solution {\npublic:\n    long largestSquareArea(vector<std::vector<int>>& bottomLeft, vector<std::vector<int>>& topRight) {\n        int ans = 0, n = bottomLeft.size();\n        for (int i = 0; i < n; i++) {\n            vector<int>& firstRectBL = bottomLeft[i];\n            vector<int>& firstRectTR = topRight[i];\n            for (int j = i + 1; j < n; j++) {\n                vector<int>& secondRectBL = bottomLeft[j];\n                vector<int>& secondRectTR = topRight[j];\n                if (secondRectBL[0] >= firstRectTR[0] || secondRectTR[0] <= firstRectBL[0]) continue;\n                if (secondRectTR[1] <= firstRectBL[1] || secondRectBL[1] >= firstRectTR[1]) continue;\n                int pntAx = max(firstRectBL[0], secondRectBL[0]);\n                int pntAy = max(firstRectBL[1], secondRectBL[1]);\n                int pntBx = min(firstRectTR[0], secondRectTR[0]);\n                int pntBy = min(firstRectTR[1], secondRectTR[1]);\n                int sideA = pntBx - pntAx;\n                int sideB = pntBy - pntAy;\n                ans = max(ans, min(sideA, sideB));\n            }\n        }\n        return static_cast<long>(ans) * ans;\n    }\n};\n\n```\n```python3 []\nclass Solution:\n    def largestSquareArea(self, bottomLeft: List[List[int]], topRight: List[List[int]]) -> int:\n        ans = 0\n        n = len(bottomLeft)\n        for i in range(n):\n            firstRectBL = bottomLeft[i]\n            firstRectTR = topRight[i]\n            for j in range(i + 1, n):\n                secondRectBL = bottomLeft[j]\n                secondRectTR = topRight[j]\n                if secondRectBL[0] >= firstRectTR[0] or secondRectTR[0] <= firstRectBL[0]:\n                    continue\n                if secondRectTR[1] <= firstRectBL[1] or secondRectBL[1] >= firstRectTR[1]:\n                    continue\n                pntAx = max(firstRectBL[0], secondRectBL[0])\n                pntAy = max(firstRectBL[1], secondRectBL[1])\n                pntBx = min(firstRectTR[0], secondRectTR[0])\n                pntBy = min(firstRectTR[1], secondRectTR[1])\n                sideA = pntBx - pntAx\n                sideB = pntBy - pntAy\n                ans = max(ans, min(sideA, sideB))\n        return ans ** 2        \n\n```\n\n```python []\n\nclass Solution:\n    class Solution(object):\n    def largestSquareArea(self, bottomLeft, topRight):\n        ans = 0\n        n = len(bottomLeft)\n        for i in range(n):\n            firstRectBL = bottomLeft[i]\n            firstRectTR = topRight[i]\n            for j in range(i + 1, n):\n                secondRectBL = bottomLeft[j]\n                secondRectTR = topRight[j]\n                if secondRectBL[0] >= firstRectTR[0] or secondRectTR[0] <= firstRectBL[0]:\n                    continue\n                if secondRectTR[1] <= firstRectBL[1] or secondRectBL[1] >= firstRectTR[1]:\n                    continue\n                pntAx = max(firstRectBL[0], secondRectBL[0])\n                pntAy = max(firstRectBL[1], secondRectBL[1])\n                pntBx = min(firstRectTR[0], secondRectTR[0])\n                pntBy = min(firstRectTR[1], secondRectTR[1])\n                sideA = pntBx - pntAx\n                sideB = pntBy - pntAy\n                ans = max(ans, min(sideA, sideB))\n        return ans ** 2\n        \n\n```\n\n![image.png](https://assets.leetcode.com/users/images/c6464ff3-e20a-4309-adba-edf3d5153c5f_1708837156.0702188.png)\n
['Python', 'C++', 'Java', 'Python3']