【leetcode 5425. 切割后面积最大的蛋糕】Python 解题思路

题目链接:
解题思路:
找到横切和竖切的最大间隔,然后相乘

代码:

class Solution:
    def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
        horizontalCuts.sort()
        verticalCuts.sort()
        n_hori=len(horizontalCuts)
        n_vert=len(verticalCuts)
        if n_hori>1:
            diff_hori=[horizontalCuts[i+1]-horizontalCuts[i] for i in range(n_hori-1)]
            diff_hori.append(horizontalCuts[0])
            diff_hori.append(h-horizontalCuts[-1])
        else:
            diff_hori=[horizontalCuts[0]]
            diff_hori.append(h-horizontalCuts[0])
        if n_vert>1:
            diff_vert=[verticalCuts[i+1]-verticalCuts[i] for i in range(n_vert-1)]
            diff_vert.append(verticalCuts[0])
            diff_vert.append(w-verticalCuts[-1])
        else:
            diff_vert=[verticalCuts[0]]
            diff_vert.append(w-verticalCuts[0])

        return max(diff_hori)*max(diff_vert)%(10**9+7)

你可能感兴趣的:(leetcode)