bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 42. Trapping Rain Water

42. Trapping Rain Water

Leetcode Solutions

Using Pointers

  1. Initialize left pointer to 0 and right pointer to the last index of the array.
  2. Initialize left_max and right_max to 0.
  3. Initialize ans to 0 to store the total water trapped.
  4. While left is less than right, do: a. If height[left] is less than height[right], then: i. If height[left] is greater than or equal to left_max, update left_max. ii. Else, add left_max - height[left] to ans. iii. Increment left. b. Else: i. If height[right] is greater than or equal to right_max, update right_max. ii. Else, add right_max - height[right] to ans. iii. Decrement right.
  5. Return ans.
UML Thumbnail

Dynamic Programming

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR