Skip to main content

Two Bars2

Question

Two bar problem Second

find best area among ....

Solution

two_pointers/two_bars2.py


def solution(arr):
left_id = 0
right_id = len(arr) - 1

max_area = 0

while left_id < right_id:
area = min(arr[left_id], arr[right_id]) * (right_id - left_id)
max_area = max(max_area, area)

#print(left_id, right_id, arr[left_id], arr[right_id], area)

if arr[left_id] >= arr[right_id]:
right_id = right_id - 1
else:
left_id = left_id + 1

return max_area


arr = [2, 1, 8]
result = solution(arr)

#print(result)
Stepleft_idright_idarr[left_id]arr[right_id]area
102284
212181
result
4