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

You have a car with an unlimited gas tank and it costs cost[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.

一种做法是以每一个站为起点推断是否可以在行走一个周期后回到自身。

AC code:

class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int res = 0, i = res, n = gas.size(), left = 0;
while (res<n)
{
left = left + gas[i] - cost[i];
if (left<0)
{
res++;
left = 0;
i = res;
}
else
{
i = (++i) % n;
if (i == res)
return res;
} }
return -1;
}
};

事实上还有更优化的方法。在推断以当前网站res为起始网站是否可以回到当前网站的过程中,假设在网站i出现不能到达i+1网站的情况,那么以从当前网站res到A直接的全部网站为起始点,都不能跨越过i~i+1这个坎。可以这样理解这句话。对于res~i之间的随意网站x,汽车从res出发到达x剩余的油量大于等于0,所以汽车从res出发到达i剩余的油量大于等于从网站x出发到达i剩余的油量。

基于上面的结论,能够知道。下一个可能的起始点就是i+1。我们直接让res等于i+1,再继续运行程序,直到res等于n,此时说明不存在满足条件的网站。

优化后的方法时间复杂度O(n)。

AC code:

class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int res = 0, i = res, n = gas.size(), left = 0;
while (res<n)
{
left = left + gas[i] - cost[i];
if (left<0)
{
if(i>res)
res=i;
else
res++;
left = 0;
i = res;
}
else
{
i = (++i) % n;
if (i == res)
return res;
} }
return -1;
}
};

leetcode 刷题之路 68 Gas Station的更多相关文章

  1. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  2. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  3. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  4. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  5. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

  6. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  7. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

  8. #leetcode刷题之路6- Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L     C     I ...

  9. leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

随机推荐

  1. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  2. 强制停止及删除(卸载)Windows服务

    1. 安装服务: CMD 打开命令行窗口:C:\> 运行:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe& ...

  3. hdu2042

    #include <stdio.h> int main(){ int t,i,n,res; while(~scanf("%d",&t)){ while(t--) ...

  4. PTA 10-排序4 统计工龄 (20分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每 ...

  5. Python 频繁读取Mysql相关问题

    1.需要频繁select大量数据,时间长.消耗内存大,如何解决mysql性能问题? 如果对返回的结果数量没有要求,可以控制返回的数量: cursor.fetchmany(size=1000) 这样是只 ...

  6. Codevs 1026 逃跑的拉尔夫

     时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警察局,并且 ...

  7. Xcode打包应用为ipa

    Xcode教程 Xcode4发布测试 打包Archive操作是本文要介绍的内容,发布测试的最后一步打包(Archive),Xcode4帮助文档有比较详细介绍,但是居然是错的,这里说明一下. 1.设置& ...

  8. jQuery插件封装系列(一)—— 金额录入框

    基于jQuery原型封装数值录入框,禁止录入.粘贴非数值字符 (function ($) { // 数值输入框 $.fn.numbox = function (options) { var type ...

  9. vba功能语句

    VBA语句集(第1辑) 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明Option Private Module '标记模块为私有,仅对同一工程中其它模块有用,在宏 ...

  10. python--错误了就需要调试(异常处理)

    python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 我们可打开idle-->F1进行查看文档,里面很多异常类型,如图: ...