Created
July 7, 2021 08:34
-
-
Save schauhan232/44ee05957a37bfb101aedfbb838b134e to your computer and use it in GitHub Desktop.
GeeksForGeeks: Trapping Rain Water
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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