From b8cb2fa1d42bc871f7c8312357e3afd76ad655cf Mon Sep 17 00:00:00 2001 From: Nimish Srivastava <54949793+nimishh7@users.noreply.github.com> Date: Sun, 6 Oct 2024 16:07:29 +0530 Subject: [PATCH 01/15] Added gas_station.py --- data_structures/arrays/gas_station.py | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 data_structures/arrays/gas_station.py diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py new file mode 100644 index 000000000000..fb6e88bf9593 --- /dev/null +++ b/data_structures/arrays/gas_station.py @@ -0,0 +1,32 @@ +from typing import List + +class Solution: + def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: + # Step 1: Initialize variables + # total_gas will keep track of the total balance of gas - cost across all stations + # current_gas will track the gas balance for the current trip from the starting station + # start_station will track the potential starting point of the circuit + total_gas = 0 + current_gas = 0 + start_station = 0 + + # Step 2: Loop through each gas station + for i in range(len(gas)): + # Calculate the net gas gain/loss at the current station + total_gas += gas[i] - cost[i] + current_gas += gas[i] - cost[i] + + # Step 3: If current_gas becomes negative, it means we cannot continue + # the journey from the current start_station, so we update the start_station + # to the next one and reset the current_gas to 0. + if current_gas < 0: + start_station = i + 1 # Move the starting station to the next one + current_gas = 0 # Reset the gas for the new start + + # Step 4: Check if the total gas is enough to complete the circuit + # If total_gas is negative, it means the entire circuit cannot be completed + if total_gas < 0: + return -1 + else: + # If total_gas is non-negative, return the start_station as the answer + return start_station From 4eb7761136201d1491ec58562d53115314c8a3dc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 10:40:26 +0000 Subject: [PATCH 02/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/gas_station.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index fb6e88bf9593..75a23d4e16b7 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,5 +1,6 @@ from typing import List + class Solution: def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: # Step 1: Initialize variables @@ -9,13 +10,13 @@ def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: total_gas = 0 current_gas = 0 start_station = 0 - + # Step 2: Loop through each gas station for i in range(len(gas)): # Calculate the net gas gain/loss at the current station total_gas += gas[i] - cost[i] current_gas += gas[i] - cost[i] - + # Step 3: If current_gas becomes negative, it means we cannot continue # the journey from the current start_station, so we update the start_station # to the next one and reset the current_gas to 0. From e6853f893751b3b2a99ec067cc618fd0abf53a74 Mon Sep 17 00:00:00 2001 From: Nimish Srivastava <54949793+nimishh7@users.noreply.github.com> Date: Sun, 6 Oct 2024 16:37:08 +0530 Subject: [PATCH 03/15] Update gas_station.py --- data_structures/arrays/gas_station.py | 29 ++++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index 75a23d4e16b7..e23dc68be051 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,33 +1,34 @@ -from typing import List - +from typing import list class Solution: - def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: + def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: # Step 1: Initialize variables - # total_gas will keep track of the total balance of gas - cost across all stations - # current_gas will track the gas balance for the current trip from the starting station - # start_station will track the potential starting point of the circuit total_gas = 0 current_gas = 0 start_station = 0 - + # Step 2: Loop through each gas station for i in range(len(gas)): # Calculate the net gas gain/loss at the current station total_gas += gas[i] - cost[i] current_gas += gas[i] - cost[i] - + # Step 3: If current_gas becomes negative, it means we cannot continue - # the journey from the current start_station, so we update the start_station - # to the next one and reset the current_gas to 0. if current_gas < 0: - start_station = i + 1 # Move the starting station to the next one - current_gas = 0 # Reset the gas for the new start + start_station = i + 1 + current_gas = 0 # Step 4: Check if the total gas is enough to complete the circuit - # If total_gas is negative, it means the entire circuit cannot be completed if total_gas < 0: return -1 else: - # If total_gas is non-negative, return the start_station as the answer return start_station + + +# Example 1: +gas = [1, 2, 3, 4, 5] +cost = [3, 4, 5, 1, 2] + +solution = Solution() +print(solution.can_complete_circuit(gas, cost)) # Output: 3 + From b4ca836bd768be97420de92afffe507c6999e9c2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 11:07:31 +0000 Subject: [PATCH 04/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/gas_station.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index e23dc68be051..6e1660b54019 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,22 +1,23 @@ from typing import list + class Solution: def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: # Step 1: Initialize variables total_gas = 0 current_gas = 0 start_station = 0 - + # Step 2: Loop through each gas station for i in range(len(gas)): # Calculate the net gas gain/loss at the current station total_gas += gas[i] - cost[i] current_gas += gas[i] - cost[i] - + # Step 3: If current_gas becomes negative, it means we cannot continue if current_gas < 0: - start_station = i + 1 - current_gas = 0 + start_station = i + 1 + current_gas = 0 # Step 4: Check if the total gas is enough to complete the circuit if total_gas < 0: @@ -31,4 +32,3 @@ def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: solution = Solution() print(solution.can_complete_circuit(gas, cost)) # Output: 3 - From 28a76544efeb9fbf7b8652d6cb145331078c169a Mon Sep 17 00:00:00 2001 From: Nimish Srivastava <54949793+nimishh7@users.noreply.github.com> Date: Sun, 6 Oct 2024 16:41:50 +0530 Subject: [PATCH 05/15] Update gas_station.py changes done --- data_structures/arrays/gas_station.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index 6e1660b54019..cbd4aa22d470 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,5 +1,3 @@ -from typing import list - class Solution: def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: From 4bc83baae35debd62847edce6fc624bd3ea1f9e3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 11:12:11 +0000 Subject: [PATCH 06/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/gas_station.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index cbd4aa22d470..d43d4d0c03ff 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,4 +1,3 @@ - class Solution: def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: # Step 1: Initialize variables From 59e4d08455642bc73cf68d60b7a8dd9d4f0aafae Mon Sep 17 00:00:00 2001 From: Nimish Srivastava <54949793+nimishh7@users.noreply.github.com> Date: Sun, 6 Oct 2024 17:05:07 +0530 Subject: [PATCH 07/15] Update gas_station.py --- data_structures/arrays/gas_station.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index d43d4d0c03ff..9eeb7ddb76b4 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -29,3 +29,9 @@ def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: solution = Solution() print(solution.can_complete_circuit(gas, cost)) # Output: 3 + +# Example 2: +gas = [2, 3, 4] +cost = [3, 4, 3] + +print(solution.canCompleteCircuit(gas, cost)) # Output: -1 From 8ad0d3b65436fced80d6d773723c9350291e61f2 Mon Sep 17 00:00:00 2001 From: Nimish Srivastava <54949793+nimishh7@users.noreply.github.com> Date: Sun, 6 Oct 2024 17:13:56 +0530 Subject: [PATCH 08/15] Update gas_station.py --- data_structures/arrays/gas_station.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index 9eeb7ddb76b4..e23dc68be051 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,20 +1,22 @@ +from typing import list + class Solution: def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: # Step 1: Initialize variables total_gas = 0 current_gas = 0 start_station = 0 - + # Step 2: Loop through each gas station for i in range(len(gas)): # Calculate the net gas gain/loss at the current station total_gas += gas[i] - cost[i] current_gas += gas[i] - cost[i] - + # Step 3: If current_gas becomes negative, it means we cannot continue if current_gas < 0: - start_station = i + 1 - current_gas = 0 + start_station = i + 1 + current_gas = 0 # Step 4: Check if the total gas is enough to complete the circuit if total_gas < 0: @@ -30,8 +32,3 @@ def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: solution = Solution() print(solution.can_complete_circuit(gas, cost)) # Output: 3 -# Example 2: -gas = [2, 3, 4] -cost = [3, 4, 3] - -print(solution.canCompleteCircuit(gas, cost)) # Output: -1 From 1ed1e4bd7ff39b055a8319f7f120035ef1ff3d39 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 11:44:17 +0000 Subject: [PATCH 09/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/gas_station.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index e23dc68be051..6e1660b54019 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,22 +1,23 @@ from typing import list + class Solution: def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: # Step 1: Initialize variables total_gas = 0 current_gas = 0 start_station = 0 - + # Step 2: Loop through each gas station for i in range(len(gas)): # Calculate the net gas gain/loss at the current station total_gas += gas[i] - cost[i] current_gas += gas[i] - cost[i] - + # Step 3: If current_gas becomes negative, it means we cannot continue if current_gas < 0: - start_station = i + 1 - current_gas = 0 + start_station = i + 1 + current_gas = 0 # Step 4: Check if the total gas is enough to complete the circuit if total_gas < 0: @@ -31,4 +32,3 @@ def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: solution = Solution() print(solution.can_complete_circuit(gas, cost)) # Output: 3 - From 2150765556ee9bcd9aac24de35401c462a694866 Mon Sep 17 00:00:00 2001 From: Nimish Srivastava <54949793+nimishh7@users.noreply.github.com> Date: Sun, 6 Oct 2024 17:18:31 +0530 Subject: [PATCH 10/15] Update gas_station.py --- data_structures/arrays/gas_station.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index 6e1660b54019..3197ac56ab3f 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,23 +1,20 @@ -from typing import list - - class Solution: def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: # Step 1: Initialize variables total_gas = 0 current_gas = 0 start_station = 0 - + # Step 2: Loop through each gas station for i in range(len(gas)): # Calculate the net gas gain/loss at the current station total_gas += gas[i] - cost[i] current_gas += gas[i] - cost[i] - + # Step 3: If current_gas becomes negative, it means we cannot continue if current_gas < 0: - start_station = i + 1 - current_gas = 0 + start_station = i + 1 + current_gas = 0 # Step 4: Check if the total gas is enough to complete the circuit if total_gas < 0: @@ -32,3 +29,4 @@ def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: solution = Solution() print(solution.can_complete_circuit(gas, cost)) # Output: 3 + From 7603019d3c61b00fa8e9ad2faac4c7d794ae6efa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 11:48:52 +0000 Subject: [PATCH 11/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/gas_station.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index 3197ac56ab3f..d43d4d0c03ff 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -4,17 +4,17 @@ def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: total_gas = 0 current_gas = 0 start_station = 0 - + # Step 2: Loop through each gas station for i in range(len(gas)): # Calculate the net gas gain/loss at the current station total_gas += gas[i] - cost[i] current_gas += gas[i] - cost[i] - + # Step 3: If current_gas becomes negative, it means we cannot continue if current_gas < 0: - start_station = i + 1 - current_gas = 0 + start_station = i + 1 + current_gas = 0 # Step 4: Check if the total gas is enough to complete the circuit if total_gas < 0: @@ -29,4 +29,3 @@ def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: solution = Solution() print(solution.can_complete_circuit(gas, cost)) # Output: 3 - From 044f93022075f8e6cde666a6269ce8907bb3f6ce Mon Sep 17 00:00:00 2001 From: Nimish Srivastava <54949793+nimishh7@users.noreply.github.com> Date: Sun, 6 Oct 2024 17:25:58 +0530 Subject: [PATCH 12/15] Update gas_station.py --- data_structures/arrays/gas_station.py | 42 +++++++++++++++++---------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index d43d4d0c03ff..2b9edd245b85 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,31 +1,43 @@ class Solution: - def can_complete_circuit(self, gas: list[int], cost: list[int]) -> int: - # Step 1: Initialize variables + def can_omplete_circuit(self, gas: list[int], cost: list[int]) -> int: + """ + Determines the starting gas station index from which you can complete the circuit, + or returns -1 if it's not possible. + + Args: + gas (List[int]): List of gas available at each station. + cost (List[int]): List of gas costs to travel to the next station. + + Returns: + int: The index of the starting station, or -1 if no solution exists. + + Examples: + >>> solution = Solution() + >>> solution.can_omplete_circuit([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) + 3 + >>> solution.can_omplete_circuit([2, 3, 4], [3, 4, 3]) + -1 + >>> solution.can_omplete_circuit([5, 1, 2, 3, 4], [4, 4, 1, 5, 1]) + 4 + """ total_gas = 0 current_gas = 0 start_station = 0 - - # Step 2: Loop through each gas station + for i in range(len(gas)): - # Calculate the net gas gain/loss at the current station total_gas += gas[i] - cost[i] current_gas += gas[i] - cost[i] - - # Step 3: If current_gas becomes negative, it means we cannot continue + if current_gas < 0: start_station = i + 1 current_gas = 0 - # Step 4: Check if the total gas is enough to complete the circuit if total_gas < 0: return -1 else: return start_station - -# Example 1: -gas = [1, 2, 3, 4, 5] -cost = [3, 4, 5, 1, 2] - -solution = Solution() -print(solution.can_complete_circuit(gas, cost)) # Output: 3 +# Example usage with doctests +if __name__ == "__main__": + import doctest + doctest.testmod() From 1c838c7d64e925b79b39118af13a0159f8ed9fda Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 11:56:19 +0000 Subject: [PATCH 13/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/gas_station.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index 2b9edd245b85..d8c1a914a130 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -3,7 +3,7 @@ def can_omplete_circuit(self, gas: list[int], cost: list[int]) -> int: """ Determines the starting gas station index from which you can complete the circuit, or returns -1 if it's not possible. - + Args: gas (List[int]): List of gas available at each station. cost (List[int]): List of gas costs to travel to the next station. @@ -23,11 +23,11 @@ def can_omplete_circuit(self, gas: list[int], cost: list[int]) -> int: total_gas = 0 current_gas = 0 start_station = 0 - + for i in range(len(gas)): total_gas += gas[i] - cost[i] current_gas += gas[i] - cost[i] - + if current_gas < 0: start_station = i + 1 current_gas = 0 @@ -37,7 +37,9 @@ def can_omplete_circuit(self, gas: list[int], cost: list[int]) -> int: else: return start_station + # Example usage with doctests if __name__ == "__main__": import doctest + doctest.testmod() From 44a2f9259f3698fd118c3c126bd49a95e9c13d8b Mon Sep 17 00:00:00 2001 From: Nimish Srivastava <54949793+nimishh7@users.noreply.github.com> Date: Sun, 6 Oct 2024 17:33:37 +0530 Subject: [PATCH 14/15] Update gas_station.py --- data_structures/arrays/gas_station.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index d8c1a914a130..4031db42e54d 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,16 +1,13 @@ class Solution: def can_omplete_circuit(self, gas: list[int], cost: list[int]) -> int: """ - Determines the starting gas station index from which you can complete the circuit, - or returns -1 if it's not possible. - + Finds the starting station index to complete the circuit, + or returns -1 if not possible. Args: gas (List[int]): List of gas available at each station. cost (List[int]): List of gas costs to travel to the next station. - Returns: int: The index of the starting station, or -1 if no solution exists. - Examples: >>> solution = Solution() >>> solution.can_omplete_circuit([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]) From d86adfc65a7d62d764b386e3700256bb57eef9fd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Oct 2024 12:03:58 +0000 Subject: [PATCH 15/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/gas_station.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/gas_station.py b/data_structures/arrays/gas_station.py index 4031db42e54d..15c11142fdf8 100644 --- a/data_structures/arrays/gas_station.py +++ b/data_structures/arrays/gas_station.py @@ -1,7 +1,7 @@ class Solution: def can_omplete_circuit(self, gas: list[int], cost: list[int]) -> int: """ - Finds the starting station index to complete the circuit, + Finds the starting station index to complete the circuit, or returns -1 if not possible. Args: gas (List[int]): List of gas available at each station.