| 1 | class TwoPointers: |
| 2 | # Move Zeroes using Two Pointers |
| 3 | def move_zeroes_two_pointers(self, nums): |
| 4 | left = 0 # Pointer for placing non-zero elements |
| 5 | |
| 6 | # Iterate with right pointer |
| 7 | for right in range(len(nums)): |
| 8 | if nums[right] != 0: |
| 9 | # Swap elements if right pointer finds a non-zero |
| 10 | nums[left], nums[right] = nums[right], nums[left] |
| 11 | left += 1 # Move left pointer forward |
| 12 | |
| 13 | # Brute Force approach for Container with Most Water |
| 14 | def max_area_brute_force(self, height): |
| 15 | n = len(height) |
| 16 | max_area = 0 |
| 17 | |
| 18 | # Check all pairs (i, j) |
| 19 | for i in range(n): |
| 20 | for j in range(i + 1, n): |
| 21 | # Compute the minimum height and width |
| 22 | min_height = min(height[i], height[j]) |
| 23 | width = j - i |
| 24 | area = min_height * width # Compute water contained |
| 25 | |
| 26 | max_area = max(max_area, area) # Update max water |
| 27 | return max_area |
| 28 | |
| 29 | # Two Pointers approach for Container with Most Water |
| 30 | def max_area_two_pointers(self, height): |
| 31 | left, right = 0, len(height) - 1 |
| 32 | max_area = 0 |
| 33 | |
| 34 | # Move pointers toward each other |
| 35 | while left < right: |
| 36 | width = right - left # Distance between lines |
| 37 | min_height = min(height[left], height[right]) # Compute height |
| 38 | area = min_height * width # Compute water contained |
| 39 | |
| 40 | max_area = max(max_area, area) # Update max water |
| 41 | |
| 42 | # Move the pointer pointing to the shorter height |
| 43 | if height[left] < height[right]: |
| 44 | left += 1 # Move left pointer forward |
| 45 | else: |
| 46 | right -= 1 # Move right pointer backward |
| 47 | |
| 48 | return max_area |
nothing calls this directly
no outgoing calls
no test coverage detected