CSharp · Console
// Find the maximum possible amount of water in the pool of given profile.
// Overfilled water leaks to the left side of the picture.
// The program reads the sequence of non-negative integer column heights
// until the height -1 that means last column (always having infinite height).
// Then it should output maximum number of cells that could be filled with water.
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Please enter the height of the column.(-1 will be the last column)");
// Inductive extension:
int maxColumnHeight = 0; // Height of highest column entered so far
int r = 0; // Sum of water cells stored so far
int nextColumnHeight = Convert.ToInt32(Console.ReadLine());
// invariant: inductive extension holds
while (nextColumnHeight != -1)
{
if (maxColumnHeight < nextColumnHeight)
{
maxColumnHeight = nextColumnHeight;
}
else
{
r += maxColumnHeight - nextColumnHeight;
}
nextColumnHeight = Convert.ToInt32(Console.ReadLine());
}
// Time complexity: O(n)
Console.WriteLine("Water capacity = " + r);
}
}