📜  水上游戏

📅  最后修改于: 2022-05-13 01:57:59.208000             🧑  作者: Mango

水上游戏

给定一个空杯子,这个杯子必须装满水,任务是找出杯子在任何时候都能容纳的最大水量。

给定条件:

例子:

Input: N = 4,
        0 1
        0 1
        0 1
        1 3
Output: 3
Explanation: 
    The glass initially has 0 ml of water. 
    The maximum value is obtained 
    after the first 3 operations.

Input: N = 2
        1 15
        1 24
Output: 39

方法该方法可以简单地说为每当 T 为 0 时将体积相加并找到该体积的最大值。

执行:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// returns the largest volume
int largest_volume(int n, int *t, int *x)
{
  
    // arbitrarily large value
    int minValue = 100000000;
  
    // stores the maximum
    int maxValue = 0;
  
    // Current Volume of the glass
    int c = 0;
  
    for (int i = 0; i < n; i++) 
    {
  
        // if 1st operation is performed
        if (t[i] == 0) 
        {
  
            // increment with current x
            c += x[i];
  
            // take current max
            maxValue = max(maxValue, c);
        }
  
        // if 2nd operation is performed
        else 
        {
  
            // decrement with current x
            c -= x[i];
  
            // take current min
            minValue = min(minValue, c);
        }
    }
  
    // returns the largest difference
    return maxValue - minValue;
}
  
// Driver code
int main()
{
    int n = 4;
  
    int t[4] = {0};
    int x[4] = {0};
  
    t[0] = 0;
    x[0] = 1;
  
    t[1] = 0;
    x[1] = 1;
  
    t[2] = 0;
    x[2] = 1;
  
    t[3] = 1;
    x[3] = 3;
  
    // Find the largest volume
    int ans = largest_volume(n, t, x);
  
    // Print the largest volume
    cout<< ans;
    return 0;
}
  
// This code is contributed by PrinciRaj1992


Java
// Java implementation of the approach
  
class GFG {
  
    // returns the largest volume
    static int largest_volume(int n, int[] t, int[] x)
    {
  
        // arbitrarily large value
        int min = 100000000;
  
        // stores the maximum
        int max = 0;
  
        // Current Volume of the glass
        int c = 0;
  
        for (int i = 0; i < n; i++) {
  
            // if 1st operation is performed
            if (t[i] == 0) {
  
                // increment with current x
                c += x[i];
  
                // take current max
                max = Math.max(max, c);
            }
  
            // if 2nd operation is performed
            else {
  
                // decrement with current x
                c -= x[i];
  
                // take current min
                min = Math.min(min, c);
            }
        }
  
        // returns the largest difference
        return max - min;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 4;
  
        int[] t = new int[4];
        int[] x = new int[4];
  
        t[0] = 0;
        x[0] = 1;
  
        t[1] = 0;
        x[1] = 1;
  
        t[2] = 0;
        x[2] = 1;
  
        t[3] = 1;
        x[3] = 3;
  
        // Find the largest volume
        int ans = largest_volume(n, t, x);
  
        // Print the largest volume
        System.out.println(ans);
    }
}


Python3
# Python3 implementation of the approach 
  
# Returns the largest volume 
def largest_volume(n, t, x): 
  
    # Arbitrarily large value 
    Min = 100000000
  
    # Stores the maximum 
    Max = 0
  
    # Current Volume of the glass 
    c = 0
  
    for i in range(0, n): 
  
        # If 1st operation is performed 
        if t[i] == 0: 
  
            # Increment with current x 
            c += x[i] 
  
            # Take current max 
            Max = max(Max, c) 
  
        # If 2nd operation is performed 
        else: 
              
            # Decrement with current x 
            c -= x[i] 
  
            # Take current min 
            Min = min(Min, c) 
  
    # Returns the largest difference 
    return Max - Min
  
# Driver code 
if __name__ == "__main__":
      
    n = 4
  
    t = [0, 0, 0, 1]
    x = [1, 1, 1, 3] 
  
    # Find the largest volume 
    ans = largest_volume(n, t, x) 
  
    # Print the largest volume 
    print(ans) 
      
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System; 
  
class GFG 
{ 
  
    // returns the largest volume 
    static int largest_volume(int n, int[] t, int[] x) 
    { 
  
        // arbitrarily large value 
        int min = 100000000; 
  
        // stores the maximum 
        int max = 0; 
  
        // Current Volume of the glass 
        int c = 0; 
  
        for (int i = 0; i < n; i++)
        { 
  
            // if 1st operation is performed 
            if (t[i] == 0)
            { 
  
                // increment with current x 
                c += x[i]; 
  
                // take current max 
                max = Math.Max(max, c); 
            } 
  
            // if 2nd operation is performed 
            else 
            { 
  
                // decrement with current x 
                c -= x[i]; 
  
                // take current min 
                min = Math.Min(min, c); 
            } 
        } 
  
        // returns the largest difference 
        return max - min; 
    } 
  
    // Driver code 
    public static void Main() 
    { 
        int n = 4; 
  
        int[] t = new int[4]; 
        int[] x = new int[4]; 
  
        t[0] = 0; 
        x[0] = 1; 
  
        t[1] = 0; 
        x[1] = 1; 
  
        t[2] = 0; 
        x[2] = 1; 
  
        t[3] = 1; 
        x[3] = 3; 
  
        // Find the largest volume 
        int ans = largest_volume(n, t, x); 
  
        // Print the largest volume 
        Console.WriteLine(ans); 
    } 
} 
  
// This code is contributed by 
// tufan_gupta2000


输出:
3