📜  除去一系列水平和垂直条之后,可能的最大面积

📅  最后修改于: 2021-04-17 16:42:47             🧑  作者: Mango

给定一个网格,该网格由大小为(N + 2)x(M + 2)的水平和垂直条以及两个数组H []V []表示需要删除的水平和垂直条数组成,因此任务是找到删除一系列垂直和水平条时的最大面积。

例子:

方法:请按照以下步骤解决问题:

  • 初始化两个集s1和s2以存储整数。
  • 迭代范围[1,N + 1]并将每个整数存储在s1中
  • 类似地,在[1,M +1]范围内进行迭代,并将每个整数存储在s2中
  • 遍历数组H []并从s1中删除所有H [i]
  • 同样,遍历数组V []并从s2中删除所有V [i]
  • 将更新的s1s2集转换为列表l1l2。
  • 以升序对两个列表进行排序。
  • 遍历列表l1l2 ,并将两个邻居之间的最大距离分别存储为maxHmaxV
  • maxHmaxV的乘积打印为最大区域。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the largest area
// when a series of horizontal &
// vertical bars are removed
void largestArea(int N, int M, int H[],
                 int V[], int h, int v)
{
 
  // Stores all bars
  set s1;
  set s2;
 
  // Insert horizontal bars
  for (int i = 1; i <= N + 1; i++)
    s1.insert(i);
 
  // Insert vertictal bars
  for (int i = 1; i <= M + 1; i++)
    s2.insert(i);
 
  // Remove horizontal separators from s1
  for (int i = 0; i < h; i++) {
 
    s1.erase(H[i]);
  }
 
  // Remove vertical separators from s2
  for (int i = 0; i < v; i++) {
 
    s2.erase(V[i]);
  }
 
  // Stores left out horizontal and
  // vertical separators
  int list1[s1.size()];
  int list2[s2.size()];
 
  int i = 0;
  for (auto it1 = s1.begin(); it1 != s1.end(); it1++) 
  {
    list1[i++] = *it1;
  }
 
  i = 0;
  for (auto it2 = s2.begin(); it2 != s2.end(); it2++) 
  {
    list2[i++] = *it2;
  }
 
  // Sort both list in
  // ascending order
  sort(list1, list1 + s1.size());
  sort(list2, list2 + s2.size());
 
  int maxH = 0, p1 = 0, maxV = 0, p2 = 0;
 
  // Find maximum difference of neighbors of list1
  for (int j = 0; j < s1.size(); j++) {
    maxH = max(maxH, list1[j] - p1);
    p1 = list1[j];
  }
 
  // Find max difference of neighbors of list2
  for (int j = 0; j < s2.size(); j++) {
    maxV = max(maxV, list2[j] - p2);
    p2 = list2[j];
  }
 
  // Print largest volume
  cout << (maxV * maxH) << endl;
}
 
// Driver code
int main()
{
 
  // Given value of N & M
  int N = 3, M = 3;
 
  // Given arrays
  int H[] = { 2 };
  int V[] = { 2 };
 
  int h = sizeof(H) / sizeof(H[0]);
  int v = sizeof(V) / sizeof(V[0]);
 
  // Function call to find the largest
  // area when a series of horizontal &
  // vertical bars are removed
  largestArea(N, M, H, V, h, v);
 
  return 0;
}
 
// This code is contributed by divyeshrabadiya07.


Java
// Java program for the above approach
 
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Function to find the largest area
    // when a series of horizontal &
    // vertical bars are removed
    static void largestArea(int N, int M,
                            int[] H, int[] V)
    {
        // Stores all bars
        Set s1 = new HashSet<>();
        Set s2 = new HashSet<>();
 
        // Insert horizontal bars
        for (int i = 1; i <= N + 1; i++)
            s1.add(i);
 
        // Insert vertictal bars
        for (int i = 1; i <= M + 1; i++)
            s2.add(i);
 
        // Remove horizontal separators from s1
        for (int i = 0; i < H.length; i++) {
 
            s1.remove(H[i]);
        }
 
        // Remove vertical separators from s2
        for (int i = 0; i < V.length; i++) {
 
            s2.remove(V[i]);
        }
 
        // Stores left out horizontal and
        // vertical separators
        int[] list1 = new int[s1.size()];
        int[] list2 = new int[s2.size()];
 
        int i = 0;
        Iterator it1 = s1.iterator();
        while (it1.hasNext()) {
            list1[i++] = (int)it1.next();
        }
 
        i = 0;
        Iterator it2 = s2.iterator();
        while (it2.hasNext()) {
            list2[i++] = (int)it2.next();
        }
 
        // Sort both list in
        // ascending order
        Arrays.sort(list1);
        Arrays.sort(list2);
 
        int maxH = 0, p1 = 0, maxV = 0, p2 = 0;
 
        // Find maximum difference of neighbors of list1
        for (int j = 0; j < list1.length; j++) {
            maxH = Math.max(maxH, list1[j] - p1);
            p1 = list1[j];
        }
 
        // Find max difference of neighbors of list2
        for (int j = 0; j < list2.length; j++) {
            maxV = Math.max(maxV, list2[j] - p2);
            p2 = list2[j];
        }
 
        // Print largest volume
        System.out.println(maxV * maxH);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given value of N & M
        int N = 3, M = 3;
 
        // Given arrays
        int[] H = { 2 };
        int[] V = { 2 };
 
        // Function call to find the largest
        // area when a series of horizontal &
        // vertical bars are removed
        largestArea(N, M, H, V);
    }
}


Python3
# Python 3 program for the above approach
 
# Function to find the largest area
# when a series of horizontal &
# vertical bars are removed
def largestArea(N, M, H,
                 V, h, v):
 
  # Stores all bars
  s1 = set([]);
  s2 = set([]);
 
  # Insert horizontal bars
  for i in range(1, N + 2):
    s1.add(i);
 
  # Insert vertictal bars
  for i in range(1, M + 2):
    s2.add(i);
 
  # Remove horizontal separators from s1
  for i in range(h):
    s1.remove(H[i]);
 
  # Remove vertical separators from s2
  for i in range( v ):
 
    s2.remove(V[i]);
 
  # Stores left out horizontal and
  # vertical separators
  list1 = [0] * len(s1)
  list2 = [0]*len(s2);
 
  i = 0;
  for it1 in s1:
    list1[i] = it1;
    i += 1
 
  i = 0;
  for it2 in s2:
    list2[i] = it2
    i += 1
 
  # Sort both list in
  # ascending order
  list1.sort();
  list2.sort();
 
  maxH = 0
  p1 = 0
  maxV = 0
  p2 = 0;
 
  # Find maximum difference of neighbors of list1
  for j in range(len(s1)):
    maxH = max(maxH, list1[j] - p1);
    p1 = list1[j];
 
  # Find max difference of neighbors of list2
  for j in range(len(s2)):
    maxV = max(maxV, list2[j] - p2);
    p2 = list2[j];
 
  # Print largest volume
  print((maxV * maxH))
 
# Driver code
if __name__ == "__main__":
 
  # Given value of N & M
  N = 3
  M = 3;
 
  # Given arrays
  H = [2]
  V = [2];
 
  h = len(H)
  v = len(V);
 
  # Function call to find the largest
  # area when a series of horizontal &
  # vertical bars are removed
  largestArea(N, M, H, V, h, v);
 
  # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to find the largest area
    // when a series of horizontal &
    // vertical bars are removed
    static void largestArea(int N, int M,
                            int[] H, int[] V)
    {
        // Stores all bars
        HashSet s1 = new HashSet();
        HashSet s2 = new HashSet();
  
        // Insert horizontal bars
        for (int i = 1; i <= N + 1; i++)
            s1.Add(i);
  
        // Insert vertictal bars
        for (int i = 1; i <= M + 1; i++)
            s2.Add(i);
  
        // Remove horizontal separators from s1
        for (int i = 0; i < H.Length; i++) {
  
            s1.Remove(H[i]);
        }
  
        // Remove vertical separators from s2
        for (int i = 0; i < V.Length; i++) {
  
            s2.Remove(V[i]);
        }
  
        // Stores left out horizontal and
        // vertical separators
        int[] list1 = new int[s1.Count];
        int[] list2 = new int[s2.Count];
  
        int I = 0;
        foreach(int it1 in s1)
        {
            list1[I++] = it1;
        }
  
        I = 0;
        foreach(int it2 in s2)
        {
            list2[I++] = it2;
        }
  
        // Sort both list in
        // ascending order
        Array.Sort(list1);
        Array.Sort(list2);
  
        int maxH = 0, p1 = 0, maxV = 0, p2 = 0;
  
        // Find maximum difference of neighbors of list1
        for (int j = 0; j < list1.Length; j++) {
            maxH = Math.Max(maxH, list1[j] - p1);
            p1 = list1[j];
        }
  
        // Find max difference of neighbors of list2
        for (int j = 0; j < list2.Length; j++) {
            maxV = Math.Max(maxV, list2[j] - p2);
            p2 = list2[j];
        }
  
        // Print largest volume
        Console.WriteLine(maxV * maxH);
    }
     
  // Driver code
  static void Main()
  {
     
    // Given value of N & M
    int N = 3, M = 3;
 
    // Given arrays
    int[] H = { 2 };
    int[] V = { 2 };
 
    // Function call to find the largest
    // area when a series of horizontal &
    // vertical bars are removed
    largestArea(N, M, H, V);
  }
}
 
// This code is contributed by divyesh072019.


输出:
4

时间复杂度: max(N,M)*(log(max(N,M)))
辅助空间:O(max(N,M))