Thursday 12 May 2016

Challenge #1 - Crossing the bridge at night (C++ implementation)






Statement:
Mr. Algorithm and his friends like to party but all the party spots are across the bridge from where Mr. Algorithm live. The bridge is quite old unsafe, so only two people at a time can cross it and they need a lamp to do so at night. Unfortunately, his group only has one lamp.
This particular night is very cold, so you want to cross the bridge as fast as possible. For the i-th person in your group you know the time ti it will take them to cross the bridge. When two people i and j cross the bridge, they walk at the speed of the slowest of them, so it would take max(ti, tj) to get other side.

Calculate the minimal time your group will need to cross the bridge.

Input:
1 <= number_of_Mr.Algorithm_friends(include_him) <= 100
0 <= speed_for_each_person[i] <= 10^9

Output:
The minimum time required to cross the bridge.

Test case #1:
Input: [1, 2, 5, 10]
Output: 17

Test case #2:
Input: [1, 2, 3, 4, 5]
Output: 16

Test case #3:
Input: [1, 2, 3]
Output: 6

Test case #4:
Input: 3858721
Output: 3858721

Test case #5:
Input: [9, 1, 8, 2, 7, 3, 6, 4, 5, 9, 1, 8, 2, 7, 3, 6, 4, 5, 9, 1, 8, 2, 7, 3, 6, 4, 5, 9, 1, 8, 2, 7, 3, 6, 4, 5, 9, 1, 8, 2, 7, 3, 6, 4, 5, 9, 1, 8, 2, 7, 3, 6, 4, 5, 9, 1, 8, 2, 7, 3, 6, 4, 5, 9, 1, 8, 2, 7, 3, 6, 4, 5]
Ouput: 285

Test case #6:
Input: [1001, 2000, 2005, 2006]
Output: 8013

Test case #7:
Input: [1, 2, 10000, 0, 7]
Ouput: 10005

Test case #8:
Input: [239239239, 1, 239239239, 2]
Output: 239239246


HINT

Check the solution below(PayPal button):

No comments:
Write comments