From db38f0e514318e404d434bb84ea517164b42b392 Mon Sep 17 00:00:00 2001 From: Merdo Date: Wed, 29 Jan 2025 13:57:34 +0300 Subject: [PATCH 1/5] Solution for Bridge and Torch Problem --- dynamic_programming/brige_torch_problem.py | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 dynamic_programming/brige_torch_problem.py diff --git a/dynamic_programming/brige_torch_problem.py b/dynamic_programming/brige_torch_problem.py new file mode 100644 index 000000000..ac5ba42f0 --- /dev/null +++ b/dynamic_programming/brige_torch_problem.py @@ -0,0 +1,31 @@ +#person_pace = [1, 2, 5, 10,12,16] +person_pace = list(map(int, input().split())) +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) From 0175a2be868eef67d6abd393da1abdbbf2b578d2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 Jan 2025 11:03:37 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/brige_torch_problem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/brige_torch_problem.py b/dynamic_programming/brige_torch_problem.py index ac5ba42f0..8e9cf2082 100644 --- a/dynamic_programming/brige_torch_problem.py +++ b/dynamic_programming/brige_torch_problem.py @@ -1,4 +1,4 @@ -#person_pace = [1, 2, 5, 10,12,16] +# person_pace = [1, 2, 5, 10,12,16] person_pace = list(map(int, input().split())) person_pace.sort() # Sort the array for optimal pairing people = len(person_pace) From 0de43acf24c156c9fec79c15679ba75c8fa3d88c Mon Sep 17 00:00:00 2001 From: Merdo Date: Thu, 30 Jan 2025 00:59:01 +0300 Subject: [PATCH 3/5] example input added --- dynamic_programming/brige_torch_problem.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dynamic_programming/brige_torch_problem.py b/dynamic_programming/brige_torch_problem.py index ac5ba42f0..d45d30fb0 100644 --- a/dynamic_programming/brige_torch_problem.py +++ b/dynamic_programming/brige_torch_problem.py @@ -1,5 +1,4 @@ -#person_pace = [1, 2, 5, 10,12,16] -person_pace = list(map(int, input().split())) +person_pace = [1, 2, 5, 10,12,16] person_pace.sort() # Sort the array for optimal pairing people = len(person_pace) From 297da1f274ed4fd5ec4757c867d800e975c1610e Mon Sep 17 00:00:00 2001 From: Merdo Date: Thu, 30 Jan 2025 01:04:10 +0300 Subject: [PATCH 4/5] input added --- dynamic_programming/brige_torch_problem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/brige_torch_problem.py b/dynamic_programming/brige_torch_problem.py index 349abb8c5..d45d30fb0 100644 --- a/dynamic_programming/brige_torch_problem.py +++ b/dynamic_programming/brige_torch_problem.py @@ -1,4 +1,4 @@ - person_pace = [1, 2, 5, 10,12,16] +person_pace = [1, 2, 5, 10,12,16] person_pace.sort() # Sort the array for optimal pairing people = len(person_pace) From 455749a9eb839eb836cfe63478eb43cf0f83340c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 29 Jan 2025 22:04:47 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/brige_torch_problem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/brige_torch_problem.py b/dynamic_programming/brige_torch_problem.py index d45d30fb0..e72cdc1c7 100644 --- a/dynamic_programming/brige_torch_problem.py +++ b/dynamic_programming/brige_torch_problem.py @@ -1,4 +1,4 @@ -person_pace = [1, 2, 5, 10,12,16] +person_pace = [1, 2, 5, 10, 12, 16] person_pace.sort() # Sort the array for optimal pairing people = len(person_pace)