Maximum Area of a Piece of Cake | JS, Python, Java, C++ | Simple Sort & Iterate Solution w/ Expl.
(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\n\n---\n\n#### Idea:\n
*(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful,* ***please upvote*** *this post.)*\n\n---\n\n#### ***Idea:***\n\nThe trick to this problem is realizing that if the horizontal slices and vertical slices are perpendicular, then all vertical slices cross all horizontal slices. This means that we just need to find the largest of each, and the cross-section should be the largest slice.\n\nTo find the largest slice of each, we need to first **sort** the horizontal cuts (**hc**) and vertical cuts (**vc**), then iterate through both sets and keep track of the maximum difference found between two consecutive cuts (**maxh**, **maxv**). We need to not forget to include the two end cuts, which are found using **0** and **h**/**w**, as well.\n\nOnce we have the largest difference for both, we can just **return** the product of these two numbers, **modulo 1e9+7**.\n\n - _**Time Complexity: O(N * log(N) + M * log(M))**  where **N** is the length of **hc** and **M** is the length of **vc**_\n - _**Space Complexity: O(1)**_\n\n---\n\n#### ***Javascript Code:***\n```javascript\nvar maxArea = function(h, w, hc, vc) {\n    hc.sort((a,b) => a - b)\n    vc.sort((a,b) => a - b)\n    let maxh = Math.max(hc[0], h - hc[hc.length-1]),\n        maxv = Math.max(vc[0], w - vc[vc.length-1])\n    for (let i = 1; i < hc.length; i++)\n        maxh = Math.max(maxh, hc[i] - hc[i-1])\n    for (let i = 1; i < vc.length; i++)\n        maxv = Math.max(maxv, vc[i] - vc[i-1])\n    return BigInt(maxh) * BigInt(maxv) % 1000000007n\n};\n```\n\n---\n\n#### ***Python Code:***\n```python\nclass Solution:\n    def maxArea(self, h: int, w: int, hc: List[int], vc: List[int]) -> int:\n        hc.sort()\n        vc.sort()\n        maxh, maxv = max(hc[0], h - hc[-1]), max(vc[0], w - vc[-1])\n        for i in range(1, len(hc)):\n            maxh = max(maxh, hc[i] - hc[i-1])\n        for i in range(1, len(vc)):\n            maxv = max(maxv, vc[i] - vc[i-1])\n        return (maxh * maxv) % 1000000007\n```\n\n---\n\n#### ***Java Code:***\n```java\nclass Solution {\n    public int maxArea(int h, int w, int[] hc, int[] vc) {\n        Arrays.sort(hc);\n        Arrays.sort(vc);\n        int maxh = Math.max(hc[0], h - hc[hc.length-1]),\n            maxv = Math.max(vc[0], w - vc[vc.length-1]);\n        for (int i = 1; i < hc.length; i++)\n            maxh = Math.max(maxh, hc[i] - hc[i-1]);\n        for (int i = 1; i < vc.length; i++)\n            maxv = Math.max(maxv, vc[i] - vc[i-1]);\n        return (int)((long)maxh * maxv % 1000000007);\n    }\n}\n```\n\n---\n\n#### ***C++ Code:***\n```c++\nclass Solution {\npublic:\n    int maxArea(int h, int w, vector<int>& hc, vector<int>& vc) {\n        sort(hc.begin(), hc.end());\n        sort(vc.begin(), vc.end());\n        int maxh = max(hc[0], h - hc.back()),\n            maxv = max(vc[0], w - vc.back());\n        for (int i = 1; i < hc.size(); i++)\n            maxh = max(maxh, hc[i] - hc[i-1]);\n        for (int i = 1; i < vc.size(); i++)\n            maxv = max(maxv, vc[i] - vc[i-1]);\n        return (int)((long)maxh * maxv % 1000000007);\n    }\n};\n```
[]