Skip to content

Commit b71f628

Browse files
committed
docs(algorithms, greedy): gas stations
1 parent 635dff6 commit b71f628

17 files changed

Lines changed: 83 additions & 20 deletions

algorithms/dynamic_programming/buy_sell_stock/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,3 @@ Output: 6
126126
- Arrays
127127
- Dynamic Programming
128128
- Greedy
129-
130-

algorithms/dynamic_programming/buy_sell_stock/__init__.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
def max_profit(prices: List[int]) -> int:
66
"""
77
Find the maximum profit that can be made from buying and selling a stock once
8+
9+
Time complexity is O(n) as we iterate through each price to get the maximum profit
10+
Space complexity is O(1) as no extra space is used
11+
Args:
12+
prices(list): list of prices
13+
Returns:
14+
int: maximum profit that can be made
815
"""
916
if prices is None or len(prices) < 2:
1017
return 0
1118

1219
start_price = prices[0]
1320
current_max_profit = 0
1421

15-
for price in prices:
16-
if price < start_price:
17-
start_price = price
18-
elif price - start_price > current_max_profit:
19-
current_max_profit = price - start_price
22+
for price in prices[1:]:
23+
start_price = min(start_price, price)
24+
current_max_profit = max(current_max_profit, price - start_price)
2025

2126
return current_max_profit
2227

@@ -28,13 +33,14 @@ def max_profit_two_pointers(prices: List[int]) -> int:
2833
Space: O(1), no extra memory is used
2934
Time: O(n), where n is the size of the input list & we iterate through the list only once
3035
"""
31-
if prices is None or len(prices) < 2:
36+
number_of_prices = len(prices)
37+
if prices is None or number_of_prices < 2:
3238
return 0
3339

3440
left, right = 0, 1
3541
current_max_profit = 0
3642

37-
while right < len(prices):
43+
while right < number_of_prices:
3844
low = prices[left]
3945
high = prices[right]
4046

algorithms/dynamic_programming/buy_sell_stock/test_max_profit.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import unittest
22

3-
from . import max_profit, max_profit_two_pointers
3+
from algorithms.dynamic_programming.buy_sell_stock import (
4+
max_profit,
5+
max_profit_two_pointers,
6+
)
47

58

69
class MaxProfitTestCases(unittest.TestCase):

algorithms/greedy/gas_stations/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,54 @@ Therefore, you can't travel around the circuit once no matter where you start.
4242

4343
- Array
4444
- Greedy
45+
46+
## Solution
47+
48+
If there is more gas along the route than the cost of the route, then there is guaranteed to be a solution to the problem.
49+
So the first step is to check if the sum of the gas is greater than or equal to the sum of the cost. If it is not, then
50+
we return -1.
51+
52+
Next, we iterate through the gas station to find the starting index of our circuit using a greedy approach: whenever we
53+
don't have enough gas to reach the next station, we move our starting gas station to the next station and reset our gas
54+
tank.
55+
56+
We start at the first station, and fill our tank with gas[0] = 5 units of gas. From there, it takes cost[0] = 1 units of
57+
gas to travel to the next station, so we arrive at station 2 (index 1) with 4 units of gas.
58+
59+
![Solution 1](./images/solutions/gas_stations_solution_1.png)
60+
![Solution 2](./images/solutions/gas_stations_solution_2.png)
61+
![Solution 3](./images/solutions/gas_stations_solution_3.png)
62+
63+
At station 2, we fill our tank with gas[1] = 2 units of gas, for a total of 6 units of gas. It takes cost[1] = 5 units
64+
of gas to travel to the next station, so we arrive at station 3 with 1 unit of gas
65+
66+
![Solution 4](./images/solutions/gas_stations_solution_4.png)
67+
![Solution 5](./images/solutions/gas_stations_solution_5.png)
68+
69+
Now at station 3, we fill our tank with gas[2] = 0 units of gas, for a total of 1 unit of gas. It takes cost[2] = 5 units
70+
of gas to travel to the next station, which we don't have.
71+
72+
This is where our greedy approach comes in. We reset our starting station to the next station i + 1 and reset our gas
73+
tank to 0. We can do this because all other start indexes between 0 and 2 will also run into the same problem of not
74+
having enough gas to reach the next station, so we can rule them out.
75+
76+
![Solution 6](./images/solutions/gas_stations_solution_6.png)
77+
![Solution 7](./images/solutions/gas_stations_solution_7.png)
78+
79+
If we follow this approach of resetting the start index and gas tank whenever we don't have enough gas to reach the next
80+
station, then when we finish iterating, the last start index will be the solution to the problem.
81+
82+
![Solution 8](./images/solutions/gas_stations_solution_8.png)
83+
![Solution 9](./images/solutions/gas_stations_solution_9.png)
84+
![Solution 10](./images/solutions/gas_stations_solution_10.png)
85+
![Solution 11](./images/solutions/gas_stations_solution_11.png)
86+
87+
### Complexity Analysis
88+
89+
#### Time Complexity
90+
91+
O(n) where n is the number of gas stations. We only iterate through the gas stations once.
92+
93+
#### Space Complexity
94+
95+
O(1) We only use constant extra space for variables.

algorithms/greedy/gas_stations/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ def can_complete_circuit(gas: List[int], cost: List[int]) -> int:
2222

2323
gas_tank, start_index = 0, 0
2424

25-
for i in range(len(gas)):
26-
gas_tank += gas[i] - cost[i]
25+
for gas_station in range(len(gas)):
26+
# can reach next station:
27+
# update remaining fuel
28+
gas_tank += gas[gas_station] - cost[gas_station]
2729

2830
if gas_tank < 0:
29-
start_index = i + 1
31+
# can't reach next station:
32+
# try starting from next station
33+
start_index = gas_station + 1
3034
gas_tank = 0
3135
return start_index
45.1 KB
Loading
42.5 KB
Loading
32.2 KB
Loading
48.8 KB
Loading
42.6 KB
Loading

0 commit comments

Comments
 (0)