This creates the problem that my pointer ends up outside the array. But when I run my program in VS Code this doesn't happen.
LeetCode question nr. 42
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Used Array for both tests.
j = { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 }
VS Code Methode
private static int Trap(int[] height)
{
int rechts = 1;
int links = 0;
int zwischen = 0;
int erg = 0;
while (links <= height.Length - 1)
{
while (height[links] > height[links + rechts])
{
zwischen += (height[links] - height[links + rechts]);
rechts++;
if (links + rechts > height.Length - 1)
{
zwischen = 0;
rechts = 1;
links++;
}
if (links >= height.Length - 1)
{
return erg;
}
}
if (height[links] <= height[links + rechts])
{
erg += zwischen;
Console.WriteLine(" links = " + links + " rechts = " + rechts + " zwischen = " + zwischen + " erg = " + erg);
zwischen = 0;
links += rechts;
rechts = 1;
}
}
return erg;
}
Console
links = 0 rechts = 1 zwischen = 0 erg = 0
links = 1 rechts = 2 zwischen = 1 erg = 1
links = 3 rechts = 4 zwischen = 4 erg = 5
links = 8 rechts = 2 zwischen = 1 erg = 6
6
LeetCode
public int Trap(int[] height) {
int rechts = 1;
int links = 0;
int zwischen = 0;
int erg = 0;
while (links <= height.Length - 1)
{
while (height[links] > height[links + rechts])
{
zwischen += (height[links] - height[links + rechts]);
rechts++;
if (links + rechts > height.Length - 1)
{
zwischen = 0;
rechts = 1;
links++;
}
if (links >= height.Length - 1)
{
Console.WriteLine("hi");
return erg;
}
}
if (height[links] <= height[links + rechts])
{
erg += zwischen;
Console.WriteLine(" links = " + links + " rechts = " + rechts + " zwischen = " + zwischen + " erg = " + erg);
zwischen = 0;
links += rechts;
rechts = 1;
}
}
return erg;
}
Console same array
links = 0 rechts = 1 zwischen = 0 erg = 0
links = 1 rechts = 2 zwischen = 1 erg = 1
links = 3 rechts = 4 zwischen = 4 erg = 5
links = 8 rechts = 2 zwischen = 1 erg = 6
"hi"
links = 0 rechts = 5 zwischen = 9 erg = 9
Error
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
At Solution.Trap(Int32[] height)
At __Driver__.Main(String[] args)
I know that the VS Code function is static and LeetCode isnt. But both should stop executing after the return statement. I even tried (LeetCode) changing the condition of the last if statement where I add zwischen on to erg. My solution to that was that I added an boollean "fertig" in front of the retun statement. So that the last if statment doesnt execute again, but it still did.
(height[links] <= height[links + rechts] && fertig = false)
Addition I tried debugging each line ( ending )
if (links >= height.Length - 1)
{
Console.WriteLine("end1 " + erg);
return erg;
}
I let my program display the result and which return statement it uses (shown by erg).
links = 0 rechts = 1 zwischen = 0 erg = 0
links = 1 rechts = 2 zwischen = 1 erg = 1
links = 3 rechts = 4 zwischen = 4 erg = 5
links = 8 rechts = 2 zwischen = 1 erg = 6
end1 6 //Value 6 is correct. But the code doesnt terminate after return.
links = 0 rechts = 5 zwischen = 9 erg = 9
end2
Your answer works for the first input, but it's running both of the test cases. Try your code in VS with the follow input and you'll see:
height = [4,2,0,3,2,5]
If you just want a hint without the solved answer, I'd recommend adding boundary checks on the height
array, or debugging line-by-line with the array listed above..