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 ...
随机推荐
- BFS:UVa201-Squares
Squares A children's board game consists of a square array of dots that contains lines connecting so ...
- http协议工作原理(转)
WWW是以Internet作为传输媒介的一个应用系统,WWW网上最基本的传输单位是Web网页.WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间 ...
- 引用类型(JavaScript第5章)
引用类型的值(对象)是引用类型的一个实例.在ECMScript中,引用类型是一种数据结构,用于将数据和功能组织在一起. 一.Object类型 创建Object实例的方式有两种.第一种是使用new操作符 ...
- spring-cloud-sleuth 学习资源
https://www.baeldung.com/spring-cloud-sleuth-single-application https://howtodoinjava.com/spring-clo ...
- Java-从一个字符串获取子字符串
substring函数 package com.tj; public class MyClass implements Cloneable { public static void main(Stri ...
- bean容器能力
bean容器能力 对bean容器的最简单的述求 能生产bean 能注入组装bean: 字段,构造函数 spring bean容器(3.0版本)的能力列表 能生产bean 能注入组装bean:字段,构造 ...
- log4j动态日志级别调整
1. 针对root logger的设置 log4j.rootLogger=INFO, CONSOLELogger.getRootLogger().setLevel(org.apache.log4j.L ...
- 【Luogu】P2680运输计划(树上差分+二分)
题目链接 总体思路……怎么说呢……是个暴力吧…… 首先用倍增预处理出每条路径的长度. 然后按长度把路径排序. 然后二分答案.对于当前答案mid检验,怎么检验呢? 首先差分把所有长度比mid大的链上除了 ...
- BZOJ 2140 稳定婚姻 ——二分图
论二分图的可行边与必须边. 考虑用dinic增广之后的图,一些是必要的割边,一些是可行的割边. 我们首先求出一组可行的最大匹配,那么这些变都是可行的. 然后我们求一遍强连通分量. 如果 scc[u]! ...
- Bzoj1083 1083: [SCOI2005]繁忙的都市【MST】
大水题,真不知道出题者是怎么把这么水的题出的这么长的TAT 其实这题在于考语文水平,一共三个要求,前两个要求意思就是要选出的道路是树形的,最后一个要求就是要权值最小,于是整个题意说白了就是求一棵MST ...