Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for Bridge and Torch Problem #12544

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions dynamic_programming/brige_torch_problem.py
Original file line number Diff line number Diff line change
@@ -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)
Loading
Oops, something went wrong.