11. Container With Most Water
The problem requires us to find the maximum area between vertical lines of different heights at different separations.
Approach 1: Brute Force
As always, brute forcing is the first solution that strikes. For each of the lines look at all the other lines to see if they enclose an area greater than what we have already seen. Although not expected to pass, I still went ahead and tried this solution just in case the test cases weren't extremely large.
As expected, the test cases were large enough that the time limit exceeded on test case 41/49
Time Complexity: O(n²)
Space Complexity: O(1)
Approach 2: Two Ends Approach
This one took some serious help to come up with. Drawing diagrams on a whiteboard didn't seem to help since I could not get rid of the second loop
The fix to that problem was to start at the ends of the array and then each time move the end with the shorter height to the next one.
This approach ensured that the array was only looked at once and reduced the runtime by a factor of n
As expected this solution was accepted.
Time Complexity: O(n) (Only one pass)
Space Complexity: O(1)
Last updated