diff --git a/posts/two-pointers.html b/posts/two-pointers.html index 64e39e7..fcad4af 100644 --- a/posts/two-pointers.html +++ b/posts/two-pointers.html @@ -73,16 +73,19 @@
def maxArea(height: list[int]) -> int:
- area = 0
+ ans = 0
l, r = 0, len(height) - 1
+
while l < r:
width, min_height = r - l, min(height[l], height[r])
- area = max(area, width * min_height)
+ ans = max(ans, width * min_height)
+
while l < r and height[l] <= min_height:
l += 1
while l < r and height[r] <= min_height:
r -= 1
- return area
+
+ return ans
def minimum_rescue_boats(people: list[int], limit: int) -> int:
- boats = 0
- light = 0
- heavy = len(people) - 1
+ ans = 0
+ l, r = 0, len(people) - 1
people.sort()
- while light <= heavy:
- if light == heavy:
- boats += 1
+ while l <= r:
+ if l == r:
+ ans += 1
break
- elif people[light] + people[heavy] <= limit:
- boats += 1
- light += 1
- heavy -= 1
+ elif people[l] + people[r] <= limit:
+ ans += 1
+ l += 1
+ r -= 1
else:
- boats += 1
- heavy -= 1
+ ans += 1
+ r -= 1
- return boats
+ return ans