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     }
  • 相关阅读:
    程序中的对话框应用(2)- ”另存为“对话框
    程序中的对话框应用(1)- ”打开“对话框
    Delphi常见报错
    用户界面设计(7)- 窗体上的状态栏应用(StatusBar)
    用户界面设计(6)- 窗体上的工具栏应用(ToolBar)
    用户界面设计(5)- 窗体上菜单的应用
    用户界面设计(4)- 窗体的控制
    用户界面设计(3)- 窗体的主要方法
    Delphi的类和对象(十二)- (TObject、TPersistent、TComponent、TControl、TWinControl、TCustomControl、TGraphicControl、TInterfacedObject)简单介绍
    安装 Visual Stuidio 2010 失败
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10136396.html
Copyright © 2011-2022 走看看