Last active
February 12, 2023 10:14
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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