|
| 1 | +{ |
| 2 | + "problem_name": "gas_station", |
| 3 | + "solution_class_name": "Solution", |
| 4 | + "problem_number": "134", |
| 5 | + "problem_title": "Gas Station", |
| 6 | + "difficulty": "Medium", |
| 7 | + "topics": "Array, Greedy", |
| 8 | + "readme_description": "There are `n` gas stations along a circular route, where the amount of gas at the `ith` station is `gas[i]`.\n\nYou have a car with an unlimited gas tank and it costs `cost[i]` of gas to travel from the `ith` station to its next `(i + 1)th` station. You begin the journey with an empty tank at one of the gas stations.\n\nGiven two integer arrays `gas` and `cost`, return *the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return* `-1`. If there exists a solution, it is **guaranteed** to be **unique**.", |
| 9 | + "_readme_examples": { |
| 10 | + "list": [ |
| 11 | + { |
| 12 | + "content": "```\nInput: gas = [1,2,3,4,5], cost = [3,4,5,1,2]\nOutput: 3\nExplanation:\nStart at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4\nTravel to station 4. Your tank = 4 - 1 + 5 = 8\nTravel to station 0. Your tank = 8 - 2 + 1 = 7\nTravel to station 1. Your tank = 7 - 3 + 2 = 6\nTravel to station 2. Your tank = 6 - 4 + 3 = 5\nTravel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.\nTherefore, return 3 as the starting index.\n```" |
| 13 | + }, |
| 14 | + { |
| 15 | + "content": "```\nInput: gas = [2,3,4], cost = [3,4,3]\nOutput: -1\nExplanation:\nYou can't start at station 0 or 1, as there is not enough gas to travel to the next station.\nLet's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4\nTravel to station 0. Your tank = 4 - 3 + 2 = 3\nTravel to station 1. Your tank = 3 - 3 + 3 = 3\nYou cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.\nTherefore, you can't travel around the circuit once no matter where you start.\n```" |
| 16 | + } |
| 17 | + ] |
| 18 | + }, |
| 19 | + "readme_constraints": "- `n == gas.length == cost.length`\n- `1 <= n <= 10^5`\n- `0 <= gas[i], cost[i] <= 10^4`\n- The input is generated such that the answer is unique.", |
| 20 | + "helpers_imports": "", |
| 21 | + "helpers_content": "", |
| 22 | + "helpers_run_name": "can_complete_circuit", |
| 23 | + "helpers_run_signature": "(solution_class: type, gas: list[int], cost: list[int])", |
| 24 | + "helpers_run_body": " implementation = solution_class()\n return implementation.can_complete_circuit(gas, cost)", |
| 25 | + "helpers_assert_name": "can_complete_circuit", |
| 26 | + "helpers_assert_signature": "(result: int, expected: int) -> bool", |
| 27 | + "helpers_assert_body": " assert result == expected\n return True", |
| 28 | + "solution_imports": "", |
| 29 | + "solution_contents": "", |
| 30 | + "solution_class_content": "", |
| 31 | + "test_imports": "import pytest\nfrom leetcode_py import logged_test\nfrom .helpers import assert_can_complete_circuit, run_can_complete_circuit\nfrom .solution import Solution", |
| 32 | + "test_content": "", |
| 33 | + "test_class_name": "GasStation", |
| 34 | + "test_class_content": " def setup_method(self):\n self.solution = Solution()", |
| 35 | + "_solution_methods": { |
| 36 | + "list": [ |
| 37 | + { |
| 38 | + "name": "can_complete_circuit", |
| 39 | + "signature": "(self, gas: list[int], cost: list[int]) -> int", |
| 40 | + "body": " # TODO: Implement can_complete_circuit\n return -1" |
| 41 | + } |
| 42 | + ] |
| 43 | + }, |
| 44 | + "_test_helper_methods": { |
| 45 | + "list": [{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }] |
| 46 | + }, |
| 47 | + "_test_methods": { |
| 48 | + "list": [ |
| 49 | + { |
| 50 | + "name": "test_can_complete_circuit", |
| 51 | + "signature": "(self, gas: list[int], cost: list[int], expected: int)", |
| 52 | + "parametrize": "gas, cost, expected", |
| 53 | + "test_cases": "[([1,2,3,4,5], [3,4,5,1,2], 3), ([2,3,4], [3,4,3], -1), ([1,2], [2,1], 1), ([5], [4], 0), ([2], [2], 0), ([1,2,3], [3,3,3], -1), ([3,1,1], [1,2,2], 0), ([5,1,2,3,4], [4,4,1,5,1], 4), ([1,2,3,4,5,5,70], [2,3,4,3,9,6,2], 6), ([4,5,2,6,5,3], [3,2,7,3,2,9], -1), ([6,1,4,3,5], [3,8,2,4,2], 2), ([2,3,4,5], [3,4,5,6], -1)]", |
| 54 | + "body": " result = run_can_complete_circuit(Solution, gas, cost)\n assert_can_complete_circuit(result, expected)" |
| 55 | + } |
| 56 | + ] |
| 57 | + }, |
| 58 | + "playground_imports": "from helpers import run_can_complete_circuit, assert_can_complete_circuit\nfrom solution import Solution", |
| 59 | + "playground_setup": "# Example test case\ngas = [1,2,3,4,5]\ncost = [3,4,5,1,2]\nexpected = 3", |
| 60 | + "playground_run": "result = run_can_complete_circuit(Solution, gas, cost)\nresult", |
| 61 | + "playground_assert": "assert_can_complete_circuit(result, expected)" |
| 62 | +} |
0 commit comments