切割后面积最大的蛋糕

切割后面积最大的蛋糕

题记:

矩形蛋糕的高度为 h 且宽度为 w,给你两个整数数组 horizontalCutsverticalCuts,其中:

  • horizontalCuts[i] 是从矩形蛋糕顶部到第 i 个水平切口的距离
  • verticalCuts[j] 是从矩形蛋糕的左侧到第 j 个竖直切口的距离

请你按数组 horizontalCutsverticalCuts 中提供的水平和竖直位置切割后,请你找出 面积最大 的那份蛋糕,并返回其 面积 。由于答案可能是一个很大的数字,因此需要将结果 10^9 + 7 取余 后返回。

示例 1:在这里插入图片描述

输入:h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
输出:4
解释:上图所示的矩阵蛋糕中,红色线表示水平和竖直方向上的切口。切割蛋糕后,绿色的那份蛋糕面积最大。

示例 2:

在这里插入图片描述

输入:h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
输出:6
解释:上图所示的矩阵蛋糕中,红色线表示水平和竖直方向上的切口。切割蛋糕后,绿色和黄色的两份蛋糕面积最大。

示例 3:

输入:h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
输出:9

提示:

  • 2 <= h, w <= 10^9
  • 1 <= horizontalCuts.length <= min(h - 1, 10^5)
  • 1 <=verticalCuts.length <= min(w - 1, 10^5)
  • 1 <= horizontalCuts[i] < h
  • 1 <= verticalCuts[i] < w
  • 题目数据保证 horizontalCuts 中的所有元素各不相同
  • 题目数据保证 verticalCuts 中的所有元素各不相同

题目来源: https://leetcode.cn/problems/maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts/description/

解题方法:

思路:分别找到水平和竖直两两相差距离最大的值,返回最大乘积(面积)的结果即可

代码如下:

class Solution {

    /**
     * @param Integer $h
     * @param Integer $w
     * @param Integer[] $horizontalCuts
     * @param Integer[] $verticalCuts
     * @return Integer
     */
    function maxArea($h, $w, $horizontalCuts, $verticalCuts) {
        //找到水平切口距离差最大值
        $max_h = [];
        sort($horizontalCuts);  //升序排序水平切口坐标数组
        for ($i = 0; $i < count($horizontalCuts); $i++) {
            if(($i+1) < count($horizontalCuts)){
                $max_h[$i] = $horizontalCuts[$i+1] - $horizontalCuts[$i];   //找到每两两相邻相差的值
            }
        }
        sort($max_h);   //升序排序
        $h_max_value = end($max_h); //找到相差最大值
        $h_start = $horizontalCuts[0];  //第一个水平切口到原点的距离
        $h_end = $h - end($horizontalCuts); //蛋糕的高度与最后一个水平切口的距离
        $h_real_max_value = max($h_start,$h_end);
        $h_real_max_value = max($h_real_max_value,$h_max_value);    //找到真实的水平切口最大距离差值

        //找到竖直切口距离差最大值
        $max_v = [];
        sort($verticalCuts);    //升序排序竖直切口坐标数组
        for ($i = 0; $i < count($verticalCuts); $i++){
            if(($i + 1) < count($verticalCuts)){
                $max_v[$i] = $verticalCuts[$i + 1] - $verticalCuts[$i];     //找到每两两相邻相差的值
            }
        }
        sort($max_v);   //升序排序
        $v_max_value = end($max_v); //找到相差最大值
        $v_start = $verticalCuts[0];    //第一个竖直切口到原点的距离
        $v_end = $w - end($verticalCuts);   //蛋糕的宽度与最后一个竖直切口的距离
        $v_real_max_value = max($v_start,$v_end);
        $v_real_max_value = max($v_real_max_value,$v_max_value);    //找到真实的竖直切口最大距离差值

        return ($h_real_max_value * $v_real_max_value) % (1e9 + 7);     //返回最大切口面积
    }
}

$h = 5; 
$w = 4; 
$horizontalCuts = [1,2,4]; 
$verticalCuts = [1,3];
$s = new Solution();
$res = $s->maxArea($h, $w, $horizontalCuts, $verticalCuts);
print_r($res);

//输出示例:4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值