Skip to content

Commit db38f0e

Browse files
author
Merdo
committed
Solution for Bridge and Torch Problem
1 parent 6c92c5a commit db38f0e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#person_pace = [1, 2, 5, 10,12,16]
2+
person_pace = list(map(int, input().split()))
3+
person_pace.sort() # Sort the array for optimal pairing
4+
people = len(person_pace)
5+
6+
# Base cases
7+
if people == 1:
8+
print("Min Time to Cross is:", person_pace[0])
9+
elif people == 2:
10+
print("Min Time to Cross is:", person_pace[1])
11+
else:
12+
total_time = 0
13+
while people > 3:
14+
# Strategy 1: Send the two fastest first, then the fastest returns
15+
option1 = person_pace[1] + person_pace[0] + person_pace[-1] + person_pace[1]
16+
# Strategy 2: Send the two slowest, then the fastest returns
17+
option2 = person_pace[-1] + person_pace[0] + person_pace[-2] + person_pace[0]
18+
19+
# Choose the minimum of the two strategies
20+
total_time += min(option1, option2)
21+
22+
# Remove the two slowest people who have crossed
23+
person_pace = person_pace[:-2]
24+
25+
# Handle the last 2 or 3 people
26+
if len(person_pace) == 3:
27+
total_time += person_pace[2] + person_pace[0] + person_pace[1]
28+
elif len(person_pace) == 2:
29+
total_time += person_pace[1]
30+
31+
print("Min Time to Cross is:", total_time)

0 commit comments

Comments
 (0)