title: Struggling to debug java solution to leetcode algorithm
body: <p>I was trying to solve this problem here: <a href="https://leetcode.com/problems/trapping-rain-water/#/description" rel="nofollow noreferrer">https://leetcode.com/problems/trapping-rain-water/#/description</a></p>
  
  <p>And my code is providing incorrect answers, but I can't understand why. When I look at it and I run it through my head, I can't figure out what's wrong with it.</p>
  
  <p>Here is my solution:
  (Please don't provide me with information about a more efficient methodology if possible, I want to try and figure that out on my own).</p>
  
  <pre><code>public class Solution {
      public int trap(int[] height) {
          int totalWaterTrapped = 0;
          for (int i = 0; i &lt; height.length; i++){
              if (i == 0){
                  continue;
              } else if (i == (height.length - 1)){
                  continue;
              } else {
                  int tallestLeftwardHeight = height[i];
                  for (int x = i; x &gt;= 0; x--){
                      if (height[x] &gt; tallestLeftwardHeight){
                          tallestLeftwardHeight = x;
                      }
                  }
                  int tallestRightwardHeight = height[i];
                  for (int y = i; y &lt; height.length; y++){
                      if (height[y] &gt; tallestRightwardHeight){
                          tallestRightwardHeight = y;
                      }
                  }
  
                  totalWaterTrapped += (Math.min(tallestLeftwardHeight, tallestRightwardHeight) - height[i]);
              }
          }
          return totalWaterTrapped;
      }
  }
  </code></pre>
  
  <p>Thank you kindly for any help.</p>