Two Bars2
Question
Two bar problem Second
find best area among ....
Solution
- Full
- Simple
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)
Step | left_id | right_id | arr[left_id] | arr[right_id] | area |
---|---|---|---|---|---|
1 | 0 | 2 | 2 | 8 | 4 |
2 | 1 | 2 | 1 | 8 | 1 |
result |
---|
4 |
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)
Step | left_id | right_id | arr[left_id] | arr[right_id] | area |
---|---|---|---|---|---|
1 | 0 | 2 | 2 | 8 | 4 |
2 | 1 | 2 | 1 | 8 | 1 |
result |
---|
4 |