Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2edcffe

Browse files
Create Solution.py
1 parent f9844bd commit 2edcffe

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
3+
dp = [startFuel] + [0] * len(stations) # Initialize dp
4+
5+
for i, (loc, cap) in enumerate(stations): # Traversing the gas station
6+
for t in range(i, -1, -1):
7+
if dp[t] >= loc: # If the t fuel, can reach the position of the current gas station.
8+
dp[t+1] = max(dp[t+1], dp[t] + cap) # Attempt to update dp
9+
10+
for i, d in enumerate(dp): # Traversing a minimum number of times
11+
if d >= target: return i
12+
return -1
13+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /