Skip to content

Instantly share code, notes, and snippets.

@schauhan232
Created July 7, 2021 08:34
Show Gist options
  • Save schauhan232/44ee05957a37bfb101aedfbb838b134e to your computer and use it in GitHub Desktop.
Save schauhan232/44ee05957a37bfb101aedfbb838b134e to your computer and use it in GitHub Desktop.
GeeksForGeeks: Trapping Rain Water
public class Solution {
public int Trap(int[] height) {
return GetTotal(height);
}
private static int GetTotal(int[] height)
{
var totalLength = height.Length;
var total = 0;
for (int i = 0; i < totalLength; i++)
{
var leftMaxValue = height[i];
var rightMaxValue = height[i];
for (int left = i; left >= 0; left--)
{
var currentvalue = height[left];
leftMaxValue = Math.Max(leftMaxValue, currentvalue);
}
for (int right = i + 1; right < totalLength; right++)
{
var currentvalue = height[right];
rightMaxValue = Math.Max(rightMaxValue, currentvalue);
}
Console.WriteLine($"Left: {leftMaxValue} Right: {rightMaxValue}");
total += Math.Min(leftMaxValue, rightMaxValue) - height[i];
}
return total;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment