Skip to content

Instantly share code, notes, and snippets.

@Tr-reny
Last active February 12, 2023 10:14
Show Gist options
  • Save Tr-reny/5fcab8c4010fd707a7248faad01b9cbf to your computer and use it in GitHub Desktop.
Save Tr-reny/5fcab8c4010fd707a7248faad01b9cbf to your computer and use it in GitHub Desktop.
The code calculates the maximum distance between a field and a water tower on a wheat farm for determining the appropriate pump power. The fields and towers are modeled as sorted lists of integers representing their positions. The code uses two pointers to iterate through both lists, finding the maximum difference between a field and tower posit…
# Calculates maximum distance between a field and water tower
def max_power(fields, towers):
fields.sort()
towers.sort()
i, j = 0, 0
max_distance = 0
while i < len(fields) and j < len(towers):
# find the maximum distance between a field and a tower
max_distance = max(max_distance, abs(fields[i] - towers[j]))
# increment either i or j depending on which is smaller
if fields[i] < towers[j]:
i += 1
else:
j += 1
# return the maximum distance
return max_distance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment