Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode’s forums.

Leetcode Problem #1465 (Medium): Maximum Area of …


This content originally appeared on DEV Community and was authored by seanpgallivan

This is part of a series of Leetcode solution explanations (index). If you liked this solution or found it useful, please like this post and/or upvote my solution post on Leetcode's forums.

Leetcode Problem #1465 (Medium): Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

Description:


(Jump to: Solution Idea || Code: JavaScript | Python | Java | C++)

Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.

Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.

Examples:

Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Visual: Example 1 Visual
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Visual: Example 2 Visual
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9

Constraints:

  • 2 <= h, w <= 10^9
  • 1 <= horizontalCuts.length < min(h, 10^5)
  • 1 <= verticalCuts.length < min(w, 10^5)
  • 1 <= horizontalCuts[i] < h
  • 1 <= verticalCuts[i] < w
  • It is guaranteed that all elements in horizontalCuts are distinct.
  • It is guaranteed that all elements in verticalCuts are distinct.

Idea:


(Jump to: Problem Description || Code: JavaScript | Python | Java | C++)

The 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.

To 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.

Once we have the largest difference for both, we can just return the product of these two numbers, modulo 1e9+7.

  • Time Complexity: O(N * log(N) + M * log(M)) where N is the length of hc and M is the length of vc
  • Space Complexity: O(1)

Javascript Code:


(Jump to: Problem Description || Solution Idea)

var maxArea = function(h, w, hc, vc) {
    hc.sort((a,b) => a - b)
    vc.sort((a,b) => a - b)
    let maxh = Math.max(hc[0], h - hc[hc.length-1]),
        maxv = Math.max(vc[0], w - vc[vc.length-1])
    for (let i = 1; i < hc.length; i++)
        maxh = Math.max(maxh, hc[i] - hc[i-1])
    for (let i = 1; i < vc.length; i++)
        maxv = Math.max(maxv, vc[i] - vc[i-1])
    return (maxh * maxv) % 1000000007
};

Python Code:


(Jump to: Problem Description || Solution Idea)

class Solution:
    def maxArea(self, h: int, w: int, hc: List[int], vc: List[int]) -> int:
        hc.sort()
        vc.sort()
        maxh, maxv = max(hc[0], h - hc[-1]), max(vc[0], w - vc[-1])
        for i in range(len(hc)):
            maxh = max(maxh, hc[i] - hc[i-1])
        for i in range(len(vc)):
            maxv = max(maxv, vc[i] - vc[i-1])
        return (maxh * maxv) % 1000000007

Java Code:


(Jump to: Problem Description || Solution Idea)

class Solution {
    public int maxArea(int h, int w, int[] hc, int[] vc) {
        Arrays.sort(hc);
        Arrays.sort(vc);
        int maxh = Math.max(hc[0], h - hc[hc.length-1]),
            maxv = Math.max(vc[0], w - vc[vc.length-1]);
        for (int i = 1; i < hc.length; i++)
            maxh = Math.max(maxh, hc[i] - hc[i-1]);
        for (int i = 1; i < vc.length; i++)
            maxv = Math.max(maxv, vc[i] - vc[i-1]);
        return (int)((long)maxh * maxv % 1000000007);
    }
}

C++ Code:


(Jump to: Problem Description || Solution Idea)

class Solution {
public:
    int maxArea(int h, int w, vector<int>& hc, vector<int>& vc) {
        sort(hc.begin(), hc.end());
        sort(vc.begin(), vc.end());
        int maxh = max(hc[0], h - hc.back()),
            maxv = max(vc[0], w - vc.back());
        for (int i = 1; i < hc.size(); i++)
            maxh = max(maxh, hc[i] - hc[i-1]);
        for (int i = 1; i < vc.size(); i++)
            maxv = max(maxv, vc[i] - vc[i-1]);
        return (int)((long)maxh * maxv % 1000000007);
    }
};


This content originally appeared on DEV Community and was authored by seanpgallivan


Print Share Comment Cite Upload Translate Updates
APA

seanpgallivan | Sciencx (2021-06-03T08:16:36+00:00) Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts. Retrieved from https://www.scien.cx/2021/06/03/solution-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/

MLA
" » Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts." seanpgallivan | Sciencx - Thursday June 3, 2021, https://www.scien.cx/2021/06/03/solution-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/
HARVARD
seanpgallivan | Sciencx Thursday June 3, 2021 » Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts., viewed ,<https://www.scien.cx/2021/06/03/solution-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/>
VANCOUVER
seanpgallivan | Sciencx - » Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/03/solution-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/
CHICAGO
" » Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts." seanpgallivan | Sciencx - Accessed . https://www.scien.cx/2021/06/03/solution-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/
IEEE
" » Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts." seanpgallivan | Sciencx [Online]. Available: https://www.scien.cx/2021/06/03/solution-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/. [Accessed: ]
rf:citation
» Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts | seanpgallivan | Sciencx | https://www.scien.cx/2021/06/03/solution-maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.