Trapping Rain Water I & II: Solving Hard LeetCode Problems After Work (Day 3)
Photo by l ch on Unsplash

Day 3 was a test of willpower.

No kidding.

Free to read for non-members here

Work was brutal today.

I spent the entire day refactoring some APIs based on heavy review comments, sandwiched between back-to-back meetings.

More importantly, I didn’t get home until 8 PM.

The old me would have ordered some junk food and watched YouTube or Netflix, for sure.

But this version of me shut his eyes down for 10 minutes, prayed to God and said

“This is a privilege. To get tired doing things you like!”

I ate dinner and opened the IDE.

This morning, I sat for an hour and did

1. Insert Interval

I wanted to close the loop on the Interval topics from yesterday.

  • The Problem: Insert Interval (LeetCode 57)
  • The Takeaway: I resisted the urge to use the “Merge Interval” logic (I won’t lie, I did waste 20 minutes trying). Since the input was already sorted, you can do this in O(N) by just splitting the list into three logical parts:
  1. Left: Intervals ending before the new one starts [non overlapping]
  2. Middle: The merging phase (updating min/max) [overlapping]
  3. Right: Intervals starting after the new one ends [non overlapping]

and then I started on:

2. Container With Most Water

This is the classic intro to Two Pointers.

  • Logic: You have two lines at opposite ends. The area is determined by the shorter line. To potentially find a bigger area, you must move the shorter line inward. Moving the taller line can never help you because the width is decreasing.
  • Status: Solved quickly (coded on the commute to work)
water = (j-i)*min(height[j], height[i])

Now, after dinner I did:

3. Trapping Rain Water 1 and 2 (The Boss Levels)

These are LC-HARD

And I didn’t even know there was a 3D version of it.

For Trapping Rain Water I

I used the Two Pointer approach to track left_max and right_max.

The logic is not simple until you visualize it:

Think like water. So, water settles at the lowest boundary.
At any index i, the water level is determined by the lowest wall among the highest wall on the left and the highest wall on the right.
Because water spills over the lower side.
So the max possible water height at i is:
water level = min(maxLeft, maxRight)
But the bar at index i already occupies some height.
If the bar is tall, it reduces space for water.
So remove the bar height:
water depth = water level − height[i]
But water cannot be negative.
If the bar itself is higher than both boundaries, it holds zero water.
So you wrap it with:
max(…, 0)
Final intuitive formula
water[i] = max( min(maxLeft, maxRight) − height[i], 0 )

For Trapping Rain Water II (Harder):

This is the 3D version (LeetCode 407) where you have a grid of heights.

  • The Shift: Two pointers don’t work here because water can spill in 4 directions. You need a Min-Heap (Priority Queue).
  • The Logic: You visualize the border of the grid as a dam. Add all border cells to a Min-Heap.
  1. Pick the shortest wall from the heap (because water spills from the lowest point first).
  2. Check its neighbors. If a neighbor is shorter, water fills it up to the current wall’s height.
  3. Add the neighbor to the heap.
  4. Repeat until the heap is empty.
  5. It is essentially Dijkstra’s algorithm but for water levels.

I’ll come back to edit this later (because what I wrote here is weird — I might not understand it myself tomorrow lol)

The Health Log:

I got my blood work back this morning.

  • The Good: General markers are all good (90 parameters).
  • The Bad: Vitamin B12 is low.
  • The Reality: As a vegetarian, this is a standard issue. There aren’t many veg sources for it, and low B12 leads to brain fog which is the enemy of Coding :)
  • The Fix:
Methylcobalamin 1500mcg for 6 weeks.

I managed a 30-minute brisk walk, but I’m missing playing actual sports. The tradeoff of this 90-day roadmap is that I don’t have time for a 2-hour badminton session right now.

I have to accept that sacrifice.

Time to sleep.