Leetcode 902 Solution

This article provides solution to leetcode question 902 (minimum-number-of-refueling-stops)

https://leetcode.com/problems/minimum-number-of-refueling-stops

Solution

class Solution:
    def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
        currentFuel = startFuel

        ans = 0
        i = 0
        q = []
        while currentFuel < target:
            while i < len(stations) and currentFuel >= stations[i][0]:
                heapq.heappush(q, -stations[i][1])
                i += 1

            if not q:
                return -1

            currentFuel -= heapq.heappop(q)

            ans += 1

        return ans