import heapq def min_refuel_stops(target, start_fuel, stations): if start_fuel >= target: return 0 max_heap = [] i, n = 0, len(stations) stops = 0 max_distance = start_fuel while max_distance < target: if i < n and stations[i][0] <= max_distance: heapq.heappush(max_heap, -stations[i][1]) i += 1 elif not max_heap: return -1 else: max_distance += -heapq.heappop(max_heap) stops += 1 return stops # Driver Code def main(): input = ( (3, 3, []), (59, 14, [[9, 12], [11, 7], [13, 16], [21, 18], [47, 6]]), (15, 3, [[2, 5], [3, 1], [6, 3], [12, 6]]), (570, 140, [[140, 200], [160, 130], [310, 200], [330, 250]]), (1360, 380, [[310, 160], [380, 620], [700, 89], [850, 190], [990, 360]]) ) num = 1 for i in input: print(num, ".\tStations : ", i[2], sep="") print("\tTarget : ", i[0]) print("\tStarting fuel : ", i[1]) print("\n\tMinimum number of refueling stops :", min_refuel_stops(i[0], i[1], i[2])) num += 1 print("-" * 100, "\n") if __name__ == "__main__": main()