📜  水平和垂直切割后蛋糕的最大面积

📅  最后修改于: 2021-10-23 08:23:20             🧑  作者: Mango

给定两个正整数hw,表示形成矩形的高度h和宽度w 。此外,还有两个整数数组horizontalCutsverticalCuts ,其中水平切割[i]是从矩形顶部到第i 个水平切割的距离,同样, verticalCuts[j]是从矩形左侧到第j 个垂直切割的距离切。任务是找到矩形的最大面积 你切在阵列horizontalCutsverticalCuts提供的每个水平和垂直位置之后。由于答案可能是一个巨大的数字,因此返回此模 10^9 + 7。

例子 :

最大面积 = 6

方法:这个问题可以通过观察来解决——

  • 如果horizontalCuts 垂直于任何VerticalCut,则所有垂直切片都穿过所有horizontalCuts。
  • 接下来,矩形的最大面积必须被至少一个垂直和一个水平的切割所包围。

由上面的观察可知,我们需要分别求出两个横切和两个纵切之间的最大距离,并将它们相乘,求出矩形的面积。请按照以下步骤解决问题:

  • horizontalCutsverticalCuts数组进行排序。
  • 初始化两个变量,比如MaxHorizontalMaxVertical为 0,它们将分别存储矩形的最大水平和垂直长度。
  • 使用变量i在范围[1, horizontalCuts.size()-1] 中迭代并执行以下步骤:
    • MaxHorizontal的值修改为max(MaxHorizontal, horizontalCuts[i] – horizontalCuts[i-1])
    • MaxVertical的值修改为max(MaxVertical, verticalCuts[i] – verticalCuts[i-1])
  • 打印MaxHorizontal*MaxVertical作为答案。

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
using namespace std;
const int mod = 1e9 + 7;
 
class Solution {
public:
    // Returns the maximum area of rectangle
    // after Horizontal and Vertical Cuts
    int maxArea(int h, int w, vector& horizontalCuts,
                vector& verticalCuts)
    {
 
        // Sort the two arrays
        sort(horizontalCuts.begin(), horizontalCuts.end());
        sort(verticalCuts.begin(), verticalCuts.end());
 
        // Insert the right bound h and w
        // in their respective vectors
        horizontalCuts.push_back(h);
        verticalCuts.push_back(w);
 
        int maxHorizontal = 0;
        int maxVertical = 0;
 
        // Find the maximum Horizontal Length possible
        for (int i = 1; i < horizontalCuts.size(); i++) {
            int diff
                = horizontalCuts[i] - horizontalCuts[i - 1];
            maxHorizontal = max(maxHorizontal, diff);
        }
 
        // Find the maximum vertical Length possible
        for (int i = 1; i < verticalCuts.size(); i++) {
            int diff
                = verticalCuts[i] - verticalCuts[i - 1];
            maxVertical = max(maxVertical, diff);
        }
 
        // Return the maximum area of rectangle
        return (int)((long)maxHorizontal * maxVertical
                     % mod);
    }
};
 
// Driver Code
int main()
{
    // Class Call
    Solution ob;
   
    // Given Input
    vector hc = { 2, 5 }, vc = { 1, 3 };
    int h = 6, v = 4;
    // Function Call
    cout << (ob.maxArea(6, 4, hc, vc));
    return 0;
}


Java
// Java program for above approach
import java.awt.*;
import java.util.*;
class GFG{
 
    final int mod = (int) (1e9 + 7);
 
    // Returns the maximum area of rectangle
    // after Horizontal and Vertical Cuts
    int maxArea(int h, int w, ArrayList horizontalCuts,
                    ArrayList verticalCuts)
    {
 
        // Sort the two arrays
        Collections.sort(horizontalCuts);
        Collections.sort(verticalCuts);
 
        // Insert the right bound h and w
        // in their respective vectors
        horizontalCuts.add(h);
        verticalCuts.add(w);
 
        int maxHorizontal = 0;
        int maxVertical = 0;
 
        // Find the maximum Horizontal Length possible
        for (int i = 1; i < horizontalCuts.size(); i++) {
            int diff
                    = horizontalCuts.get(i) - horizontalCuts.get(i-1);
            maxHorizontal = Math.max(maxHorizontal, diff);
        }
 
        // Find the maximum vertical Length possible
        for (int i = 1; i < verticalCuts.size(); i++) {
            int diff
                    = verticalCuts.get(i) - verticalCuts.get(i - 1);
            maxVertical = Math.max(maxVertical, diff);
        }
 
        // Return the maximum area of rectangle
        return (int)((long)maxHorizontal * maxVertical
                % mod);
        }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Class Call
        GFG ob = new GFG();
 
        // Given Input
        ArrayList hc = new ArrayList<>();
        hc.add(2);
        hc.add(5);
        ArrayList vc = new ArrayList<>();
        vc.add(1);
        vc.add(3);
 
        int h = 6, v = 4;
       
        // Function Call
        System.out.println(ob.maxArea(6, 4, hc, vc));
    }
}
 
//This code is contributed by hritikrommie.


Python3
# python 3 Program for the above approach
mod = 1000000007
 
# Returns the maximum area of rectangle
# after Horizontal and Vertical Cuts
def maxArea(h, w, horizontalCuts,
            verticalCuts):
 
    # Sort the two arrays
    horizontalCuts.sort()
    verticalCuts.sort()
 
    # Insert the right bound h and w
    # in their respective vectors
    horizontalCuts.append(h)
    verticalCuts.append(w)
 
    maxHorizontal = 0
    maxVertical = 0
 
    # Find the maximum Horizontal Length possible
    for i in range(1, len(horizontalCuts)):
 
        diff = horizontalCuts[i] - horizontalCuts[i - 1]
        maxHorizontal = max(maxHorizontal, diff)
 
    # Find the maximum vertical Length possible
    for i in range(1,
                   len(verticalCuts)):
        diff = verticalCuts[i] - verticalCuts[i - 1]
        maxVertical = max(maxVertical, diff)
 
    # Return the maximum area of rectangle
    return (int)(maxHorizontal * maxVertical
                 % mod)
 
 
# Driver Code
if __name__ == "__main__":
 
    # Given Input
    hc = [2, 5]
    vc = [1, 3]
    h = 6
    v = 4
     
    # Function Call
    print(maxArea(6, 4, hc, vc))
 
    # This code is contributed by ukasp.


C#
// C# Program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
    static int mod = 1000000007;
     
    // Returns the maximum area of rectangle
    // after Horizontal and Vertical Cuts
    static int maxArea(int h, int w, List horizontalCuts, List verticalCuts)
    {
        // Sort the two arrays
        horizontalCuts.Sort();
        verticalCuts.Sort();
      
        // Insert the right bound h and w
        // in their respective vectors
        horizontalCuts.Add(h);
        verticalCuts.Add(w);
      
        int maxHorizontal = 0;
        int maxVertical = 0;
      
        // Find the maximum Horizontal Length possible
        for(int i = 1; i < horizontalCuts.Count; i++)
        {
      
            int diff = horizontalCuts[i] - horizontalCuts[i - 1];
            maxHorizontal = Math.Max(maxHorizontal, diff);
        }
      
        // Find the maximum vertical Length possible
        for(int i = 1; i < verticalCuts.Count; i++)
        {
            int diff = verticalCuts[i] - verticalCuts[i - 1];
            maxVertical = Math.Max(maxVertical, diff);
        }
      
        // Return the maximum area of rectangle
        return (int)(maxHorizontal * maxVertical % mod);
    }
     
  static void Main ()
  {
    // Given Input
    List hc = new List(new int[]{ 2, 5 });
    List vc = new List(new int[]{ 1, 3 });
     
    // Function Call
    Console.WriteLine(maxArea(6, 4, hc, vc));
  }
}
 
// This code is contributed by suresh07.


Javascript


输出
6

时间复杂度: O(NlogN)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程