Dado un vaso vacío, este vaso debe llenarse con agua y la tarea es encontrar la cantidad máxima de agua que ha contenido el vaso en cualquier momento.
Condiciones dadas:
El programa toma una entrada N que denota el número de pasos.
Cada paso consta de dos entradas T y X , donde T es la condición indicadora que indica si se deben verter o beber los X ml de agua en función de las siguientes condiciones:
1. si T = 0 , Vierta X ml (mililitros) de agua en el vaso
2. si T = 1 , bebe X ml de agua del vaso
Ejemplos:
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
Enfoque El enfoque puede expresarse simplemente como sumar los volúmenes siempre que T sea 0 y encontrar el máximo de este volumen.
Implementación:
C++
// C++ implementation of the approach #include<iostream> 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
Publicación traducida automáticamente
Artículo escrito por AmanKumarSingh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA