LeetCode Daily - Day 38: Container With Most Water
LeetCode Daily - Day 38: Container With Most Water
Problem statement:
We are given an array of heights, we need to select two lines such that, along with the x-axis, they form a container that holds the maximum amount of water.
And we know that the amount of water depends on the shorter height.
✅Approach 1: Brute Force
We check every possible pair of lines:
For each pair, calculate the area using the formula above
Keep track of the maximum area encountered
✅ Approach 2: Two Pointers
Start with two pointers at both ends of the array
At every step, calculate the area
Move the pointer with the smaller height
Note: Why move the smaller one?
Because we want to explore the chance of getting a taller height, which may increase the area.
This logic works in just one pass and drastically reduces the time complexity
Save this reel for quick revision and share it with your friends who are learning DSA.
Follow @codingwithyash for more such DSA concepts.
#dsa #java #leetcode #datastructure #javaprogramming #logicbuilding #codingwithyash #algorithms #maanginterview #sde #softwarengineer #collegeplacements #interviewprep
Comments
Comments from the original post on Instagram
No comments yet
