LeetCode刷题笔记(贪心):gas-station

矫情吗;* 2022-06-01 09:41 272阅读 0赞

  • 转载请注明作者和出处:http://blog.csdn.net/u011475210
  • 代码地址:https://github.com/WordZzzz/Note/tree/master/LeetCode
  • 刷题平台:https://www.nowcoder.com/ta/leetcode
  • 题  库:Leetcode经典编程题
  • 编  者:WordZzzz

    • 题目描述
    • 解题思路
    • C版代码实现

题目描述

There are N gas stations along a circular route, where the amount of gas at station i isgas[i].

You have a car with an unlimited gas tank and it costscost[i]of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.

Note:
The solution is guaranteed to be unique.

沿着一个圆形的路线有N个加油站,那里的加油站的天然气数量是gas [i]。 你有一辆带有无限制燃气罐的汽车,从车站i到下一站(i + 1)需要花费天然气。 你从一个加油站开始,开始时邮箱为空。 如果您可以在电路上巡回一次,则返回启动加油站的索引,否则返回-1。 注意:解决方案保证是唯一的。

解题思路

从start出发,如果油量足够,可以一直向后走end++;油量不够的时候,
start向后退,最终start == end的时候,如果有解一定是当前start所在位置。

因为是个环,所以我们将最后一个元素设置为起点,第一个元素设置为start下一点,这样可以用统一的判断条件start < end来作为循环的条件,当start和end两者相等时,循环结束。

C++版代码实现

  1. class Solution {
  2. public:
  3. int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
  4. if(gas.size() <= 0 || cost.size() <= 0)
  5. return -1;
  6. int start = gas.size() - 1;
  7. int end = 0;
  8. int sum = gas[start] - cost[start];
  9. while(start > end){
  10. if(sum >= 0){
  11. sum += gas[end] - cost[end];
  12. ++end;
  13. }
  14. else{
  15. --start;
  16. sum += gas[start] - cost[start];
  17. }
  18. }
  19. return sum >= 0 ? start : -1;
  20. }
  21. };

系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~

完的汪(∪。∪)。。。zzz

发表评论

表情:
评论列表 (有 0 条评论,272人围观)

还没有评论,来说两句吧...

相关阅读

    相关 LeetCode笔记

    [23. Merge k Sorted Lists][] > 要点: > > 1. 学会数据结构PriorityQueue(优先队列)的用法, 通过给优先队列传入自定义