leetcode 871. Minimum number of refueling

Topic: The car departs from the starting point to the destination, which is located target miles east of the starting position.

There are gas stations along the way, each station[i] represents a gas station, which is located at the station[i][0] mile east of the starting position, and has station[i][1] litres of gasoline.

Assume that the capacity of the car’s fuel tank is unlimited, and there is initially startFuel liters of fuel. It uses 1 liter of gasoline for every mile it drives.

When the car reaches the gas station, it may stop to refuel and transfer all gasoline from the gas station to the car.

In order to reach the destination, what is the minimum number of refueling times a car needs? If the destination cannot be reached, -1 is returned.

Note: If the remaining fuel is 0 when the car reaches the gas station, it can still be refueled there. If the remaining fuel is 0 when the car reaches the destination, it is still considered to have reached the destination.

Source: LeetCode
Link: https://leetcode-cn.com/problems/minimum-number-of-refueling-stops
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Code

class Solution {
    
    
    public int minRefuelStops(int target, int startFuel, int[][] stations) {
    
    
         PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
    
    
            @Override
            public int compare(Integer o1, Integer o2) {
    
    
                return o2-o1;
            }
        });//创建最大堆 重写compare方法,默认为最小堆
        if (startFuel>=target)//如果开始油量能直接到达
            return 0;
        if (stations.length==0)//在上条件下,没有加油站
            return -1;
        int count =0; //标记
        int x =0; //加油次数
        int dangqian =startFuel; //当前油量
        while (dangqian<target)//当前油量无法抵达时继续循环
        {
    
    
            if (dangqian >=stations[count][0])
            {
    
    //当前油量能够抵达count位置的加油站
                queue.add(stations[count][1]);//将加油站的油量存储到最大堆中
                count++;
            }
            else 
            {
    
    //当前油量无法抵达count位置加油站
                if (queue.isEmpty())
                {
    
     //如果堆中没有油量,则无法抵达终点
                    return -1;
                }
                dangqian +=queue.poll();//取出堆中最大油量
                x++;
            }
            if (count==stations.length&&(!queue.isEmpty()))
            {
    
    //如果抵达了最后一个加油站且堆不为空
                while (!queue.isEmpty())
                {
    
      //取出堆顶元素加入当前油量,直到抵达终点
                    dangqian+=queue.poll();
                    x++;
                    if (dangqian>=target)
                        return  x;
                }
            }
            if (count==stations.length&&queue.isEmpty())
                return -1;//取出了所有元素后仍然无法抵达
        }
        return x;
    }
}

We create a maximum pile, if the current amount of fuel can reach a gas station, then store it in the pile, if it can’t reach this gas station, we take the one with the largest amount of gas from the previously stored gas station and add it to the current The amount of oil until reaching the destination.
Note:
1. To create the maximum heap, you need to rewrite the compare method.
2. During the cycle, if we have arrived at the last gas station, we will not continue to take out the elements, so we need to set a separate judgment.

Guess you like

Origin blog.csdn.net/qq_45789385/article/details/102773396