📜  数组可能的矩形区域的总和

📅  最后修改于: 2021-10-25 06:50:13             🧑  作者: Mango

给定一个数组,任务是计算可以从数组元素形成的所有可能的最大面积矩形的总和。此外,您最多可以将数组的元素减少 1。

例子:

Input: a = {10, 10, 10, 10, 11, 
            10, 11, 10}
Output: 210
Explanation: 
We can form two rectangles one square (10 * 10) 
and one (11 * 10). Hence, total area = 100 + 110 = 210.

Input: a = { 3, 4, 5, 6 }
Output: 15
Explanation: 
We can reduce 4 to 3 and 6 to 5 so that we got 
rectangle of (3 * 5). Hence area = 15.

Input: a = { 3, 2, 5, 2 }
Output: 0

朴素的方法:检查数组中所有可能的四个元素,然后选择可以形成矩形的元素。在这些矩形中,将这些元素形成的面积最大的所有矩形分开。获得矩形及其面积后,将它们全部相加以获得我们想要的解决方案。

有效的方法:要得到最大面积的矩形,首先对非递增数组中的数组元素进行排序。排序后,开始选择数组元素的过程。在这里,如果数组元素相等(a[i] == a[i+1])或者较小元素 a[i+1] 的长度为1,则可以选择数组的两个元素(作为矩形的长度)小于 a[i] (在这种情况下,我们的长度为 a[i+1] 因为 a[i] 减少了 1 )。维护一个标志变量来检查我们是否同时获得长度和宽度。得到长度后,设置标志变量,现在用和长度一样的方法计算宽度,然后对矩形的面积求和。获得长度和宽度后,再次设置标志变量 false 以便我们现在搜索一个新的矩形。重复此过程,最后返回面积的最终总和。

C++
// CPP code to find sum of all
// area rectangle possible
#include 
using namespace std;
 
// Function to find
// area of rectangles
int MaxTotalRectangleArea(int a[],
                          int n)
{
    // sorting the array in
    // descending order
    sort(a, a + n, greater());
 
    // store the final sum of
    // all the rectangles area
    // possible
    int sum = 0;
    bool flag = false;
 
    // temporary variable to store
    // the length of rectangle
    int len;
     
    for (int i = 0; i < n; i++)
    {
         
        // Selecting the length of
        // rectangle so that difference
        // between any two number is 1
        // only. Here length is selected
        // so flag is set
        if ((a[i] == a[i + 1] || a[i] -
            a[i + 1] == 1) && (!flag))
        {
            // flag is set means
            // we have got length of
            // rectangle
            flag = true;
 
            // length is set to
            // a[i+1] so that if
            // a[i] a[i+1] is less
            // than by 1 then also
            // we have the correct
            // choice for length
            len = a[i + 1];
 
            // incrementing the counter
            // one time more as we have
            // considered a[i+1] element
            // also so.
            i++;
        }
 
        // Selecting the width of rectangle
        // so that difference between any
        // two number is 1 only. Here width
        // is selected so now flag is again
        // unset for next rectangle
        else if ((a[i] == a[i + 1] ||
                a[i] - a[i + 1] == 1) && (flag))
            {
                // area is calculated for
                // rectangle
                sum = sum + a[i + 1] * len;
 
                // flag is set false
                // for another rectangle
                // which we can get from
                // elements in array
                flag = false;
 
                // incrementing the counter
                // one time more as we have
                // considered a[i+1] element
                // also so.
                i++;
            }
    }
 
    return sum;
}
 
// Driver code
int main()
{
    int a[] = { 10, 10, 10, 10,
                11, 10, 11, 10,
                9, 9, 8, 8 };
    int n = sizeof(a) / sizeof(a[0]);
     
    cout << MaxTotalRectangleArea(a, n);
     
    return 0;
}


Java
// Java code to find sum of
// all area rectangle possible
import java.io.*;
import java.util.Arrays;
import java.util.*;
 
class GFG
{
    // Function to find
    // area of rectangles
    static int MaxTotalRectangleArea(Integer []a,
                                     int n)
    {
         
        // sorting the array in
        // descending order
        Arrays.sort(a, Collections.reverseOrder());
     
        // store the final sum of
        // all the rectangles area
        // possible
        int sum = 0;
        boolean flag = false;
     
        // temporary variable to
        // store the length of rectangle
        int len = 0;
         
        for (int i = 0; i < n; i++)
        {
             
            // Selecting the length of
            // rectangle so that difference
            // between any two number is 1
            // only. Here length is selected
            // so flag is set
            if ((a[i] == a[i + 1] ||
                 a[i] - a[i+1] == 1) &&
                               !flag)
            {
                // flag is set means
                // we have got length of
                // rectangle
                flag = true;
     
                // length is set to
                // a[i+1] so that if
                // a[i] a[i+1] is less
                // than by 1 then also
                // we have the correct
                // choice for length
                len = a[i+1];
     
                // incrementing the counter
                // one time more as we have
                // considered a[i+1] element
                // also so.
                i++;
            }
     
            // Selecting the width of rectangle
            // so that difference between any
            // two number is 1 only. Here width
            // is selected so now flag is again
            // unset for next rectangle
            else if ((a[i] == a[i + 1] ||
                      a[i] - a[i+1] == 1) &&
                                    (flag))
                {
                    // area is calculated for
                    // rectangle
                    sum = sum + a[i+1] * len;
     
                    // flag is set false
                    // for another rectangle
                    // which we can get from
                    // elements in array
                    flag = false;
     
                    // incrementing the counter
                    // one time more as we have
                    // considered a[i+1] element
                    // also so.
                    i++;
                }
        }
     
        return sum;
    }
     
    // Driver code
    public static void main (String args[])
    {
    Integer []a = { 10, 10, 10, 10,
                11, 10, 11, 10,
                9, 9, 8, 8 };
    int n = a.length;
     
    System.out.print(MaxTotalRectangleArea(a, n));
    }
}
// This code is contributed by
// Manish Shaw(manishshaw1)


Python3
# Python3 code to find sum
# of all area rectangle
# possible
 
# Function to find
# area of rectangles
def MaxTotalRectangleArea(a, n) :
     
    # sorting the array in
    # descending order
    a.sort(reverse = True)
 
    # store the final sum of
    # all the rectangles area
    # possible
    sum = 0
    flag = False
 
    # temporary variable to store
    # the length of rectangle
    len = 0
    i = 0
     
    while (i < n-1) :
        if(i != 0) :
            i = i + 1
             
        # Selecting the length of
        # rectangle so that difference
        # between any two number is 1
        # only. Here length is selected
        # so flag is set
        if ((a[i] == a[i + 1] or
             a[i] - a[i + 1] == 1)
              and flag == False) :
                   
            # flag is set means
            # we have got length of
            # rectangle
            flag = True
 
            # length is set to
            # a[i+1] so that if
            # a[i+1] is less than a[i]
            # by 1 then also we have
            # the correct chice for length
            len = a[i + 1]
 
            # incrementing the counter
            # one time more as we have
            # considered a[i+1] element
            # also so.
            i = i + 1
 
        # Selecting the width of rectangle
        # so that difference between any
        # two number is 1 only. Here width
        # is selected so now flag is again
        # unset for next rectangle
        elif ((a[i] == a[i + 1] or
              a[i] - a[i + 1] == 1)
                and flag == True) :
                     
            # area is calculated for
            # rectangle
            sum = sum + a[i + 1] * len
             
            # flag is set false
            # for another rectangle
            # which we can get from
            # elements in array
            flag = False
 
            # incrementing the counter
            # one time more as we have
            # considered a[i+1] element
            # also so.
            i = i + 1
         
    return sum
 
# Driver code
a = [ 10, 10, 10, 10, 11, 10,
          11, 10, 9, 9, 8, 8 ]
n = len(a)
 
print (MaxTotalRectangleArea(a, n))
 
# This code is contributed by
# Manish Shaw (manishshaw1)


C#
// C# code to find sum of all area rectangle
// possible
using System;
 
class GFG {
 
    // Function to find
    // area of rectangles
    static int MaxTotalRectangleArea(int []a,
                                       int n)
    {
         
        // sorting the array in descending
        // order
        Array.Sort(a);
        Array.Reverse(a);
     
        // store the final sum of all the
        // rectangles area possible
        int sum = 0;
        bool flag = false;
     
        // temporary variable to store the
        // length of rectangle
        int len =0;
         
        for (int i = 0; i < n; i++)
        {
             
            // Selecting the length of
            // rectangle so that difference
            // between any two number is 1
            // only. Here length is selected
            // so flag is set
            if ((a[i] == a[i + 1] || a[i] -
                a[i + 1] == 1) && (!flag))
            {
                // flag is set means
                // we have got length of
                // rectangle
                flag = true;
     
                // length is set to
                // a[i+1] so that if
                // a[i] a[i+1] is less
                // than by 1 then also
                // we have the correct
                // choice for length
                len = a[i + 1];
     
                // incrementing the counter
                // one time more as we have
                // considered a[i+1] element
                // also so.
                i++;
            }
     
            // Selecting the width of rectangle
            // so that difference between any
            // two number is 1 only. Here width
            // is selected so now flag is again
            // unset for next rectangle
            else if ((a[i] == a[i + 1] ||
                    a[i] - a[i + 1] == 1) && (flag))
                {
                    // area is calculated for
                    // rectangle
                    sum = sum + a[i + 1] * len;
     
                    // flag is set false
                    // for another rectangle
                    // which we can get from
                    // elements in array
                    flag = false;
     
                    // incrementing the counter
                    // one time more as we have
                    // considered a[i+1] element
                    // also so.
                    i++;
                }
        }
     
        return sum;
    }
     
    // Driver code
    static public void Main ()
    {
            int []a = { 10, 10, 10, 10,
                        11, 10, 11, 10,
                        9, 9, 8, 8 };
    int n = a.Length;
     
    Console.WriteLine(
                MaxTotalRectangleArea(a, n));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出

282

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

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