Binary search and greedy in Python
Binary search
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
if arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1Greedy examples
- Jump Game
- Assign Cookies
- Activity Selection
- Minimum Number of Coins
Can you explain it without the notes?
Binary search and greedy in Python
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Binary search
Turn this concept into a clear answer.
Practice this conceptAdapted from 18_DSA_PYTHON.md · “11. Binary search and greedy in Python”. Examples are study fragments; framework behavior can depend on your version.