Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts — LeetCode 1465 Python Solution

MediumGreedyArraySorting
Problem
#1465
Pattern
Greedy
Reading time
2 min

The problem

You are given a rectangular cake of size h x 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, and 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.

Example

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.

Python solution

Python
class Solution:
    def maxArea(
        self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]
    ) -> int:
        horizontalCuts.extend([0, h])
        verticalCuts.extend([0, w])
        horizontalCuts.sort()
        verticalCuts.sort()
        x = max(b - a for a, b in pairwise(horizontalCuts))
        y = max(b - a for a, b in pairwise(verticalCuts))
        return (x * y) % (10**9 + 7)

Complexity

MeasureComplexity
TimeO(m\log m + n\log n), where m and n are the lengths of `horizontalCuts` and `verticalCuts`, respectively
SpaceO(\log m + \log n) auxiliary

Pattern: Greedy

Take the locally best option every time — when you can prove that never costs you later. LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Greedy.

The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts?
LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts is rated Medium on LeetCode.
What is the time complexity of LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts?
The Python solution on this page runs in O(m\log m + n\log n), where m and n are the lengths of `horizontalCuts` and `verticalCuts`, respectively.
What is the space complexity of LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts?
The Python solution on this page uses O(\log m + \log n) auxiliary space.
What topics does LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts cover?
LeetCode 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts is tagged Greedy, Array and Sorting on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview