11. Container With Most Water
https://leetcode.com/problems/container-with-most-water/description/
Optimal Space & Time Complexity
Solutions
東
Hao
YC
SOL
Supplement / Discussion
東
White board explanation
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Hao
https://leetcode.com/problems/container-with-most-water/solutions/3493276/c-java-python-javascript-optimized-code-easy-to-understand-100-solution-explained/
- Because line (the width) consists two points, that is a scenario suitalbe with two pointers (left, right).
- The key to update iteration is
if (height[left] < height[right]) left += 1; else right -= 1;
. Though we take Math.min(height[left], height[right])
to calculate the area of rectangle, if we update the smaller height, the chance is it might be greater then the greater height in the last iteration, which brings a greater Math.min(height[left], height[right])
.
YC
- implement a left-right pointer
- move the pointer having a smaller value
SOL
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
https://medium.com/@kaokaolin456/leetcode-container-with-most-water-f463692e4533
Live Coding Space