leetcode 刷题之路 68 Gas Station
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的更多相关文章
- python -- leetcode 刷题之路
第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(二)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(一)
LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...
- #leetcode刷题之路40-组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...
- #leetcode刷题之路16-最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- #leetcode刷题之路13-罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...
- #leetcode刷题之路6- Z 字形变换
将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L C I ...
- 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 ...
随机推荐
- 【MySQL】可重复读模式下 unique key失效案例
一 [背景] 今天上午文能提笔安天下,武能上马定乾坤的登博给团队出了一道题目,谁先复现问题,奖励星巴克一杯.激起了一群忙碌的屌丝DBA的极大热情.问题是这样滴,如下图登博提示了几个细节: 1. ...
- visionPro工业视觉工具中英文一览表
- 交换机VLAN的定义、意义以及划分方式
什么是VLAN 虚拟网技术(VLAN,Virtual Local Area Network)的诞生主要源于广播.广播在网络中起着非常重要的作用,如发现新设备.调整网络路径.IP地址租赁等等,许多网络协 ...
- python基础-面向对象(类)
类 类的定义 >>> class P: ... pass ... >>> P <class __main__.P at 0x0000000001F4B ...
- linux无人值守安装介绍(一)
一.术语解释 PXE(Pre-boot ExecutionEnvironment)是由Intel设计的协议,它可以使计算机通过网络而不是从本地硬盘.光驱等设备启动.现代的网卡,一般都内嵌支持PXE的R ...
- window.location.href跳转问题
任务中遇到这样一个问题,用window.location.href跳转一到个网址,但是每次都出错,显示网址前面加上了文件所在文件夹的路径 示例如下: window.location.href=&quo ...
- 【Luogu】P2340奶牛会展
题目链接 突发奇想可以用f[i]表示智商和为i的时候情商最大是多少.这样就变成了一个背包问题. 最后更新答案的时候从0到最大背包容量遍历,最后答案是最大的i+f[i]; 但是虽然答案只能从0到m里选, ...
- mysql5.7.23版本环境配置
亲身实践安装mysql,用时居然花费了三个小时,在有那么多教程的情况下,依然在不该花费时间的路上浪费了太多时间.希望这篇文章能够帮助大家少走弯路~~ 1.下载我下载的是64位. 2.解压下载之后,我选 ...
- 刷题总结——道路覆盖(ssoj)
题目: 题目描述 Tar 把一段凹凸不平的路分成了高度不同的 N 段(每一段相同高度),并用 H[i] 表示第 i 段高度.现在 Tar 一共有 n 种泥土可用,它们都能覆盖给定的连续的 k 个部分. ...
- C++ 中头文件(.h)和源文件(.cc)的写法简述
用C++编写比较大型的项目时,文件的分割管理确实确实是非常必要的 .下面就非常简洁明了地谈谈头文件(.h)和源文件(.cc)应该怎么写. 头文件(.h):写类的声明(包括类里面的成员和方法的声明).函 ...