📜  装满水的容器

📅  最后修改于: 2021-04-26 17:58:56             🧑  作者: Mango

给定n个非负整数a_1, a_2, ..., a_n             每个代表一个坐标点(i, a_i)             。绘制’n’垂直线,使线i的两个端点位于(i, a_i)             (i, 0)
找到两条线,它们与x轴一起形成一个容器,以便该容器包含最多的水。
程序应返回一个整数,该整数对应于可以包含的最大水面积(最大面积而不是最大音量听起来很奇怪,但这是我们正在使用的2D平面)。

注意:您可能不会倾斜容器。

例子 :

Input: array = [1, 5, 4, 3]
Output: 6
Explanation : 
5 and 3 are distance 2 apart. 
So the size of the base = 2. 
Height of container = min(5, 3) = 3. 
So total area = 3 * 2 = 6

Input: array = [3, 1, 2, 4, 5]
Output: 12
Explanation : 
5 and 3 are distance 4 apart. 
So the size of the base = 4. 
Height of container = min(5, 3) = 3. 
So total area = 4 * 3 = 12

天真的解决方案:

  • 方法:这个想法很简单,涉及检查每对边界或数组元素,并找出任何一对边界下的最大面积。
  • 算法:
    1. 创建一个嵌套循环,外部循环从0到结束遍历数组(此循环的索引为i )。
    2. 内部循环从i +1遍历数组到结束(此循环的索引为j )。
    3. 找到可以包含在容器中的水,其边界高度为array [i]和array [j],即area =(j – i)* min(array [i],array [j]) ,如果该面积大于当前最大值,更新当前最大值
    4. 打印当前最大值。
  • 执行:
C++14
// C++ code for Max
// Water Container
#include 
using namespace std;
 
int maxArea(int A[], int len)
{
    int area = 0;
    for (int i = 0; i < len; i++) {
        for (int j = i + 1; j < len; j++) {
            // Calculating the max area
            area = max(area, min(A[j], A[i]) * (j - i));
        }
    }
    return area;
}
 
// Driver code
int main()
{
    int a[] = { 1, 5, 4, 3 };
    int b[] = { 3, 1, 2, 4, 5 };
 
    int len1 = sizeof(a) / sizeof(a[0]);
    cout << maxArea(a, len1);
 
    int len2 = sizeof(b) / sizeof(b[0]);
    cout << endl << maxArea(b, len2);
}


Java
// Java code for Max 
// Water Container
import java.io.*;
 
class GFG{
 
public static int maxArea(int[] a)
{
 
    int Area = 0;
     
    for(int i = 0; i < a.length; i++)
    {
        for(int j = i + 1; j < a.length; j++)
        {
            Area = Math.max(
                Area, Math.min(a[i], a[j]) *
                              (j - i));
        }
    }
    return Area;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 5, 4, 3 };
    int b[] = { 3, 1, 2, 4, 5 };
 
    System.out.println(maxArea(a));
    System.out.println(maxArea(b));
}
}
 
// This code is contributed by mulchandanimanisha5


Python3
# Python3 code for Max
# Water Container
def maxArea(A, Len) :
    area = 0
    for i in range(Len) :
        for j in range(i + 1, Len) :
           
            # Calculating the max area
            area = max(area, min(A[j], A[i]) * (j - i))
    return area
 
# Driver code
a = [ 1, 5, 4, 3 ]
b = [ 3, 1, 2, 4, 5 ]
 
len1 = len(a)
print(maxArea(a, len1))
 
len2 = len(b)
print(maxArea(b, len2))
 
# This code is contributed by divyesh072019.


C#
// C# code for Max
// Water Container
using System;
class GFG
{
 
public static int maxArea(int[] a)
{
 
    int Area = 0;
     
    for(int i = 0; i < a.Length; i++)
    {
        for(int j = i + 1; j < a.Length; j++)
        {
            Area = Math.Max(Area, Math.Min(a[i], a[j]) *
                            (j - i));
        }
    }
    return Area;
}
 
// Driver code
public static void Main(String[] args)
{
    int []a = { 1, 5, 4, 3 };
    int []b = { 3, 1, 2, 4, 5 };
 
    Console.WriteLine(maxArea(a));
    Console.Write(maxArea(b));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


C++
// C++ code for Max
// Water Container
#include
using namespace std;
 
int maxArea(int A[], int len)
{
    int l = 0;
    int r = len -1;
    int area = 0;
     
    while (l < r)
    {
        // Calculating the max area
        area = max(area, min(A[l],
                        A[r]) * (r - l));
                         
            if (A[l] < A[r])
                l += 1;
                 
            else
                r -= 1;
    }
    return area;
}
 
// Driver code
int main()
{
    int a[] = {1, 5, 4, 3};
    int b[] = {3, 1, 2, 4, 5};
     
    int len1 = sizeof(a) / sizeof(a[0]);
    cout << maxArea(a, len1);
     
    int len2 = sizeof(b) / sizeof(b[0]);
    cout << endl << maxArea(b, len2);
}
 
// This code is contributed by Smitha Dinesh Semwal


Java
// Java code for Max
// Water Container
import java.util.*;
 
class Area{
 
    public static int maxArea(int A[], int len)
    {
        int l = 0;
        int r = len -1;
        int area = 0;
     
        while (l < r)
        {
            // Calculating the max area
            area = Math.max(area,
                        Math.min(A[l], A[r]) * (r - l));
                         
            if (A[l] < A[r])
                l += 1;
                 
            else
                r -= 1;
        }
        return area;
    }
     
    public static void main(String[] args)
    {
        int a[] = {1, 5, 4, 3};
        int b[] = {3, 1, 2, 4, 5};
     
        int len1 = 4;
        System.out.print( maxArea(a, len1)+"\n" );
     
        int len2 = 5;
        System.out.print( maxArea(b, len2) );
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 code for Max
# Water Container
def maxArea( A):
    l = 0
    r = len(A) -1
    area = 0
     
    while l < r:
        # Calculating the max area
        area = max(area, min(A[l],
                        A[r]) * (r - l))
     
        if A[l] < A[r]:
            l += 1
        else:
            r -= 1
    return area
 
# Driver code
a = [1, 5, 4, 3]
b = [3, 1, 2, 4, 5]
 
print(maxArea(a))
print(maxArea(b))


C#
// C# code for Max
// Water Container
using System;
 
class Area{
 
    public static int maxArea(int []A, int len)
    {
        int l = 0;
        int r = len -1;
        int area = 0;
     
        while (l < r)
        {
            // Calculating the max area
            area = Math.Max(area,
                        Math.Min(A[l], A[r]) * (r - l));
                         
            if (A[l] < A[r])
                l += 1;
                 
            else
                r -= 1;
        }
        return area;
    }
     
    // Driver code
    public static void Main()
    {
        int []a = {1, 5, 4, 3};
        int []b = {3, 1, 2, 4, 5};
     
        int len1 = 4;
        Console.WriteLine( maxArea(a, len1));
     
        int len2 = 5;
        Console.WriteLine( maxArea(b, len2) );
    }
}
 
// This code is contributed by Vt_M


PHP


Javascript


输出:
6
12

复杂度分析:

  • 时间复杂度: O(n ^ 2)。
    由于需要数组的嵌套遍历,因此时间复杂度为O(n ^ 2)
  • 空间复杂度: O(1)。
    由于不需要额外的空间,因此空间复杂度是恒定的。

高效的解决方案:

  • 方法:给定容器边界线的高度阵列,找到可以存储在容器中的最大水量。因此,从第一个元素和最后一个元素开始,检查可以容纳的水量并存储该容器。现在出现的问题是,有没有更好的边界或线条可以包含最大的水分。因此,有一种聪明的方法可以找到它。最初,有两个索引,第一个和最后一个索引指向第一个和最后一个元素(充当容器的边界),如果值first index小于最后一个索引,则增加第一个索引,否则减少最后一个索引。由于宽度的增加是恒定的,因此通过遵循上述过程,可以达到最佳答案。
    下面的GIF说明了该方法。

  • 算法:
    1. 保留两个索引, first = 0last = n-1,以及一个存储最大面积的值max_area。
    2. 运行循环,直到第一个小于最后一个。
    3. 用max_area和min(array [first],array [last])*(last-first)的最大值更新max_area
    4. 如果array [first]的值大于array [last],则最后更新为last – 1,否则首先更新为first + 1
    5. 打印最大面积。
  • 执行:

C++

// C++ code for Max
// Water Container
#include
using namespace std;
 
int maxArea(int A[], int len)
{
    int l = 0;
    int r = len -1;
    int area = 0;
     
    while (l < r)
    {
        // Calculating the max area
        area = max(area, min(A[l],
                        A[r]) * (r - l));
                         
            if (A[l] < A[r])
                l += 1;
                 
            else
                r -= 1;
    }
    return area;
}
 
// Driver code
int main()
{
    int a[] = {1, 5, 4, 3};
    int b[] = {3, 1, 2, 4, 5};
     
    int len1 = sizeof(a) / sizeof(a[0]);
    cout << maxArea(a, len1);
     
    int len2 = sizeof(b) / sizeof(b[0]);
    cout << endl << maxArea(b, len2);
}
 
// This code is contributed by Smitha Dinesh Semwal

Java

// Java code for Max
// Water Container
import java.util.*;
 
class Area{
 
    public static int maxArea(int A[], int len)
    {
        int l = 0;
        int r = len -1;
        int area = 0;
     
        while (l < r)
        {
            // Calculating the max area
            area = Math.max(area,
                        Math.min(A[l], A[r]) * (r - l));
                         
            if (A[l] < A[r])
                l += 1;
                 
            else
                r -= 1;
        }
        return area;
    }
     
    public static void main(String[] args)
    {
        int a[] = {1, 5, 4, 3};
        int b[] = {3, 1, 2, 4, 5};
     
        int len1 = 4;
        System.out.print( maxArea(a, len1)+"\n" );
     
        int len2 = 5;
        System.out.print( maxArea(b, len2) );
    }
}
 
// This code is contributed by rishabh_jain

Python3

# Python3 code for Max
# Water Container
def maxArea( A):
    l = 0
    r = len(A) -1
    area = 0
     
    while l < r:
        # Calculating the max area
        area = max(area, min(A[l],
                        A[r]) * (r - l))
     
        if A[l] < A[r]:
            l += 1
        else:
            r -= 1
    return area
 
# Driver code
a = [1, 5, 4, 3]
b = [3, 1, 2, 4, 5]
 
print(maxArea(a))
print(maxArea(b))

C#

// C# code for Max
// Water Container
using System;
 
class Area{
 
    public static int maxArea(int []A, int len)
    {
        int l = 0;
        int r = len -1;
        int area = 0;
     
        while (l < r)
        {
            // Calculating the max area
            area = Math.Max(area,
                        Math.Min(A[l], A[r]) * (r - l));
                         
            if (A[l] < A[r])
                l += 1;
                 
            else
                r -= 1;
        }
        return area;
    }
     
    // Driver code
    public static void Main()
    {
        int []a = {1, 5, 4, 3};
        int []b = {3, 1, 2, 4, 5};
     
        int len1 = 4;
        Console.WriteLine( maxArea(a, len1));
     
        int len2 = 5;
        Console.WriteLine( maxArea(b, len2) );
    }
}
 
// This code is contributed by Vt_M

的PHP


Java脚本


输出
6
12

复杂度分析:

  • 时间复杂度: O(n)。
    由于只需要遍历数组一次,因此时间复杂度为O(n)。
  • 空间复杂度: O(1)。
    不需要额外的空间,因此空间复杂度是恒定的。