DEV Community

Discussion on: Solution: Container With Most Water

Collapse
 
problematic_dude profile image
ProblematicDude

Since any smaller height than the current height won't gonna make any difference. You can add following code inside the outer while loop ,
where

h = min(height[j],height[i])

while(height[i] <= h && i < j ) i++;
while(height[j]<= h && i < j ) j--;
Enter fullscreen mode Exit fullscreen mode

It can improve run time a lot.