12 · Two Pointers

#42 Trapping Rain Water

Hardsolve on LeetCode ↗

frequently reported atGoldman SachsWalmartSamsungAmazonByteDance / TikTokBloomberg

the problem

In plain words

Given bar heights of width 1, compute how much rain water is trapped after raining.

in height = [0,1,0,2,1,0,1,3,2,1,2,1]

out 6

in height = [4,2,0,3,2,5]

out 9

  • 1 ≤ n ≤ 2·10⁴
  • 0 ≤ height[i] ≤ 10⁵

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #11
      Container With Most Water
      Mediumin Two Pointers
    2. #238
      Product of Array Except Self
      Mediumin Arrays & Strings
    3. #84