zoukankan      html  css  js  c++  java
  • LC 871. Minimum Number of Refueling Stops 【lock, hard】

    A car travels from a starting position to a destination which is target miles east of the starting position.

    Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0]miles east of the starting position, and has station[i][1] liters of gas.

    The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives.

    When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.

    What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1.

    Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.

    经典的小车加油问题。

    【一定要注意看题,每个变量是什么意思。前几次都把stations[i][0]看成相邻两个加油站的距离了。。。】

    利用优先队列。因为要求最小的加油次数,到了一个加油站不加油,但进入优先队列,不能到达终点或者下一个加油站的时候再取最大的。

     1 class Solution {
     2 public:
     3     int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) {
     4         priority_queue<int> pq;
     5         int driven = 0;
     6         int idx = 0;
     7         int ret = 0;
     8         while (startFuel < target) {
     9             if (idx >= stations.size()) {
    10                 if(pq.empty()) return -1;
    11                 while (!pq.empty() && startFuel < target) {
    12                     startFuel += pq.top(); pq.pop();
    13                     ret++;
    14                 }
    15                 if (startFuel >= target) return ret;
    16                 else return -1;
    17             }
    18             if (startFuel >= stations[idx][0]) {
    19                 pq.push(stations[idx][1]);
    20             }
    21             else {
    22                 if (pq.empty()) return -1;
    23                 while (!pq.empty() && startFuel < stations[idx][0]) {
    24                     startFuel += pq.top(); pq.pop();
    25                     ret++;
    26                     if (startFuel >= target) return ret;
    27                 }
    28                 if(startFuel < stations[idx][0]) return -1; 
    29                 pq.push(stations[idx][1]);
    30             }
    31             idx++;
    32         }
    33         return ret;
    34     }
    35 };

    另一个种解法:

     1     int minRefuelStops(int target, int cur, vector<vector<int>> s) {
     2         int i = 0, res;
     3         priority_queue<int>pq;
     4         for (res = 0; cur < target; res++) {
     5             while (i < s.size() && s[i][0] <= cur)
     6                 pq.push(s[i++][1]);
     7             if (pq.empty()) return -1;
     8             cur += pq.top(), pq.pop();
     9         }
    10         return res;
    11     }
  • 相关阅读:
    浅谈for与for in的不同点
    mysql数据类型
    json和数组的区别
    关于html中的设置body宽高的理解
    10 件在 PHP 7 中不要做的事情
    PHP程序员的能力水平层次
    php7了解一下
    html基础
    jenkins 整合maven,svn(配置钩子程序实现提交代码自动构建),tomcat实现热部署(windows+linux分别实现)
    maven打包时包含本地jar
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10136396.html
Copyright © 2011-2022 走看看