📜  网格中不受塔限制的最大区域

📅  最后修改于: 2021-09-06 06:03:41             🧑  作者: Mango

给定两个表示网格尺寸的整数LW ,以及两个长度为N 的数组X[]Y[] ,表示网格上位置(X[i], Y[i])处的塔的数量其中(0 <= i <= N – 1)。任务是在场中找到最大的不受塔保护的无界区域。

例子:

朴素的方法:按照以下步骤解决问题:

  • 0初始化一个维度为L * W矩阵
  • 横动X []Y [],并且为每一个(X [I],Y [1])中,标记在X所有细胞[I]第i行的Y [i]由1,以表示正在把守在(X[i], Y[i])的塔旁。
  • 然后遍历矩阵和对于每个单元格,找到最大的未被保护的子矩阵,即由0组成的最大子矩阵。打印相应的区域。

时间复杂度: O(N 3 )
辅助空间: O(N 2 )

高效方法:可以使用以下贪婪技术优化上述方法:

  • 对坐标列表X[]Y[] 进行排序。
  • 计算 x 坐标之间的空格,即dX[] = {X 1 , X 2 – X 1 , …., X N – X (N-1) , (L+1) – X N } 。类似地,计算 y 坐标之间的空间, dY[] = {Y 1 , Y 2 – Y 1 , …., Y N – Y (N-1) , (W + 1) – Y N }
  • 遍历dX[]dY[]并计算它们各自的最大值。
  • 计算它们的最大值的乘积并将其打印为所需的最长无界区域。

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
#include 
using namespace std;
 
// Function to calculate the largest
// area unguarded by towers
void maxArea(int point_x[], int point_y[], int n,
             int length, int width)
{
    // Sort the x-coordinates of the list
    sort(point_x, point_x + n);
 
    // Sort the y-coordinates of the list
    sort(point_y, point_y + n);
 
    // dx --> maximum uncovered
    // tiles in x coordinates
    int dx = point_x[0];
 
    // dy --> maximum uncovered
    // tiles in y coordinates
    int dy = point_y[0];
 
    // Calculate the maximum uncovered
    // distances for both  x and y coordinates
    for (int i = 1; i < n; i++) {
        dx = max(dx, point_x[i]
                         - point_x[i - 1]);
 
        dy = max(dy, point_y[i]
                         - point_y[i - 1]);
    }
 
    dx = max(dx, (length + 1)
                     - point_x[n - 1]);
 
    dy = max(dy, (width + 1)
                     - point_y[n - 1]);
 
    // Largest unguarded area is
    // max(dx)-1 * max(dy)-1
    cout << (dx - 1) * (dy - 1);
 
    cout << endl;
}
 
// Driver Code
int main()
{
    // Length and width of the grid
    int length = 15, width = 8;
 
    // No of guard towers
    int n = 3;
 
    // Array to store the x and
    // y coordinates
    int point_x[] = { 3, 11, 8 };
    int point_y[] = { 8, 2, 6 };
 
    // Function call
    maxArea(point_x, point_y,
            n, length, width);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to calculate the largest
// area unguarded by towers
static void maxArea(int[] point_x, int[] point_y,
                    int n, int length, int width)
{
     
    // Sort the x-coordinates of the list
    Arrays.sort(point_x);
 
    // Sort the y-coordinates of the list
    Arrays.sort(point_y);
 
    // dx --> maximum uncovered
    // tiles in x coordinates
    int dx = point_x[0];
 
    // dy --> maximum uncovered
    // tiles in y coordinates
    int dy = point_y[0];
 
    // Calculate the maximum uncovered
    // distances for both  x and y coordinates
    for(int i = 1; i < n; i++)
    {
        dx = Math.max(dx, point_x[i] -
                          point_x[i - 1]);
        dy = Math.max(dy, point_y[i] -
                          point_y[i - 1]);
    }
 
    dx = Math.max(dx, (length + 1) -
                    point_x[n - 1]);
                     
    dy = Math.max(dy, (width + 1) -
                   point_y[n - 1]);
 
    // Largest unguarded area is
    // max(dx)-1 * max(dy)-1
    System.out.println((dx - 1) * (dy - 1));
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Length and width of the grid
    int length = 15, width = 8;
 
    // No of guard towers
    int n = 3;
 
    // Array to store the x and
    // y coordinates
    int point_x[] = { 3, 11, 8 };
    int point_y[] = { 8, 2, 6 };
 
    // Function call
    maxArea(point_x, point_y, n,
            length, width);
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program for the above approach
 
# Function to calculate the largest
# area unguarded by towers
def maxArea(point_x, point_y, n,
            length, width):
                 
  # Sort the x-coordinates of the list
  point_x.sort()
 
  # Sort the y-coordinates of the list
  point_y.sort()
 
  # dx --> maximum uncovered
  # tiles in x coordinates
  dx = point_x[0]
 
  # dy --> maximum uncovered
  # tiles in y coordinates
  dy = point_y[0]
 
  # Calculate the maximum uncovered
  # distances for both  x and y coordinates
  for i in range(1, n):
      dx = max(dx, point_x[i] - 
                   point_x[i - 1])
      dy = max(dy, point_y[i] -
                   point_y[i - 1])
     
  dx = max(dx, (length + 1) -
             point_x[n - 1])
              
  dy = max(dy, (width + 1) -
            point_y[n - 1])
 
  # Largest unguarded area is
  # max(dx)-1 * max(dy)-1
  print((dx - 1) * (dy - 1))
 
# Driver Code
if __name__ == "__main__":
     
  # Length and width of the grid
  length = 15
  width = 8
 
  # No of guard towers
  n = 3
 
  # Array to store the x and
  # y coordinates
  point_x = [ 3, 11, 8 ]
  point_y = [ 8, 2, 6 ]
 
  # Function call
  maxArea(point_x, point_y, n,
          length, width)
 
# This code is contributed by akhilsaini


C#
// C# Program for the above approach
using System;
 
class GFG{
 
// Function to calculate the largest
// area unguarded by towers
static void maxArea(int[] point_x, int[] point_y,
                    int n, int length, int width)
{
     
    // Sort the x-coordinates of the list
    Array.Sort(point_x);
 
    // Sort the y-coordinates of the list
    Array.Sort(point_y);
 
    // dx --> maximum uncovered
    // tiles in x coordinates
    int dx = point_x[0];
 
    // dy --> maximum uncovered
    // tiles in y coordinates
    int dy = point_y[0];
 
    // Calculate the maximum uncovered
    // distances for both  x and y coordinates
    for(int i = 1; i < n; i++)
    {
        dx = Math.Max(dx, point_x[i] -
                          point_x[i - 1]);
        dy = Math.Max(dy, point_y[i] -
                          point_y[i - 1]);
    }
    dx = Math.Max(dx, (length + 1) -
                    point_x[n - 1]);
 
    dy = Math.Max(dy, (width + 1) -
                   point_y[n - 1]);
 
    // Largest unguarded area is
    // max(dx)-1 * max(dy)-1
    Console.WriteLine((dx - 1) * (dy - 1));
}
 
// Driver Code
static public void Main()
{
     
    // Length and width of the grid
    int length = 15, width = 8;
 
    // No of guard towers
    int n = 3;
 
    // Array to store the x and
    // y coordinates
    int[] point_x = { 3, 11, 8 };
    int[] point_y = { 8, 2, 6 };
 
    // Function call
    maxArea(point_x, point_y, n,
            length, width);
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
12

时间复杂度: (N * log N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live