You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a problem I’ve memorized inside out — totally muscle memory. But I revisited it yesterday and tried to actually understand it. I even made a GIF to help myself visualize what's happening. Here's the code:
29
30
30
31
Let's use the example: [0,1,0,2,1,0,1,3,2,1,2,1]
@@ -43,14 +44,14 @@ When our pointer reaches the end of the string (or the two walls meet), that mea
43
44
44
45
Take this specific frame as an example:
45
46
46
-
47
-
At this point, the max water we can hold is leftmax = 2, but the current column has height 1, so we can trap:
47
+
At this point, the max water we can hold is leftmax = 2, but the current column has height 1, so we can trap:
48
48
49
49
`2 - 1 = 1` unit of water.
50
50
51
51
If we were comparing leftmax and rightmax directly, we wouldn’t know why this particular column can hold water. The only reason it can trap water is because its height is less than or equal to leftmax.
0 commit comments