Friday, 1 February 2019

LeetCode Q011 Container With Most Water

LeetCode Q011 Container With Most Water



Brute Force is O(n * n) time complexity

Using two pointers to improve, the trick is how to move the pointers and make sure in the way we will not miss the possibility of getting max area

i = 0, j = height.count, move i right and j left
 We always move the shorter point ,since the area is determined by the width and the shorter line
 When move i right or j left, we are decreasing the width
 And height is determined by shorter line, if move the longer point we are not able to find a better result.
So move the shorter point until find one that is longer than another pointer, calculate the area

https://github.com/Sylvia-YiyinShen/Algorithm/blob/master/TwoPointers/Q011MostWaterMaxArea.playground/Contents.swift



No comments:

Post a Comment