Binary search and greedy in Python
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn 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
Adapted from 18_DSA_PYTHON.md · “11. Binary search and greedy in Python”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.