Merge 455749a9eb839eb836cfe63478eb43cf0f83340c into 338cbafe0d5b07d57f83060ea0f9ba3a6c1155e7

This commit is contained in:
mertmarik 2025-02-09 21:06:43 +01:00 committed by GitHub
commit 22bff1d8b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,30 @@
person_pace = [1, 2, 5, 10, 12, 16]
person_pace.sort() # Sort the array for optimal pairing
people = len(person_pace)
# Base cases
if people == 1:
print("Min Time to Cross is:", person_pace[0])
elif people == 2:
print("Min Time to Cross is:", person_pace[1])
else:
total_time = 0
while people > 3:
# Strategy 1: Send the two fastest first, then the fastest returns
option1 = person_pace[1] + person_pace[0] + person_pace[-1] + person_pace[1]
# Strategy 2: Send the two slowest, then the fastest returns
option2 = person_pace[-1] + person_pace[0] + person_pace[-2] + person_pace[0]
# Choose the minimum of the two strategies
total_time += min(option1, option2)
# Remove the two slowest people who have crossed
person_pace = person_pace[:-2]
# Handle the last 2 or 3 people
if len(person_pace) == 3:
total_time += person_pace[2] + person_pace[0] + person_pace[1]
elif len(person_pace) == 2:
total_time += person_pace[1]
print("Min Time to Cross is:", total_time)