fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var f = new int[] {2, 0, 2};
  10. Console.WriteLine("{0}", GetWater(f.ToList()));
  11. }
  12.  
  13. public static int GetWater(List<int> h)
  14. {
  15. if (h == null || h.Count < 2) return 0;
  16. int n = h.Count;
  17. var left = new int[n];
  18. var right = new int[n];
  19.  
  20. int held = 0;
  21.  
  22. left[0] = h[0];
  23. for (int i = 1; i < n; i++)
  24. left [i] = Math.Max(left[i-1], h[i]);
  25.  
  26. right[n-1] = h[n-1];
  27. for (int i = n-2; i >= 0; i--)
  28. right[i] = Math.Max(right[i+1], h[i]);
  29.  
  30. for (int i =0; i < n; i++)
  31. held += Math.Min(left[i], right[i]) - h[i];
  32.  
  33. return held;
  34. }
  35. }
Success #stdin #stdout 0.02s 16964KB
stdin
Standard input is empty
stdout
2