leetcode134
class Solution {
public:
inline int get_next(int idx, int size)
{
return idx == size- ? : idx+;
}
int aux(int idx, vector<int>& gas, vector<int>& cost)
{
int i = idx;
int left = gas[i] - cost[i];
if(left < )
return -;
i = get_next(i, gas.size());
while( i!= idx)
{
left = left + gas[i] - cost[i];
if(left < )
return -;
i = get_next(i, gas.size());
}
return idx;
}
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int ret;
int i = ;
for(; i<gas.size(); ++i)
{
if(gas[i] >= cost[i])
{
ret = aux(i, gas, cost);
cout << ret << endl;
if(ret == -)
continue;
else
break;
}
}
return ret;
}
};
补充一个python的实现:
class Solution:
def canCompleteCircuit(self, gas: 'List[int]', cost: 'List[int]') -> int:
n = len(gas)
total =
sums =
begin =
for i in range(n):
diff = gas[i] - cost[i]
total += diff
sums += diff
if sums < :
begin = i +
sums =
if total < :
return -
return begin
题目确保,如果存在解,是唯一的解,因此程序只需要找出可以完成全程的第一个起点就行。
因为全程的加油数量和耗油数量都是固定的,因此从哪个点计算全程的总的消耗都是一样的。不需要先找到起点,然后再进行计算。
因此可以在一次循环中,完成两种计算:1判断是否可以走完全程(total是否小于0),2寻找第一个可以作为起点的坐标(begin)。
leetcode134的更多相关文章
- [Swift]LeetCode134. 加油站 | Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- LeetCode134:Gas Station
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- leetcode134 Gas Station
思路: https://leetcode.com/problems/gas-station/discuss/269604/Java-Greedy-thought-process 关键是要想清楚如果从加 ...
- Leetcode134. Gas Station加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...
- leetcode134:3sum
题目描述 给出一个有n个元素的数组S,S中是否有元素a,b,c满足a+b+c=0?找出数组S中所有满足条件的三元组. 注意: 三元组(a.b.c)中的元素必须按非降序排列.(即a≤b≤c) 解集中不能 ...
随机推荐
- CTF竞赛(简介)
一.解体模式(Jeopardy) 可通过互联网或现场网络参与,解决网络安全技术挑战题目,并以分值和时间排名. 题目主要包括:逆向,漏洞挖掘与利用,web渗透,密码,取证,隐写,安全编程 二.攻防模式( ...
- bool dfs 解决单一解问题的优越性
dfs的返回值类型可以是int 或者 void .bool 由void 与 int 作为返回值类型的dfs在得到解之后不能立即返回,即使你加上语句if(key)return;也要在得到解之后一点点返 ...
- (转)Linux 定时关机、休眠命令
立刻关机:sudo haltsudo init 0 sudo shutdown -h nowsudo shutdown -h 0....定时/延时关机:sudo shutdown -h 19:3019 ...
- webstorm快捷键汇总
查找替换 Webstorm快捷键 Eclipse快捷键 说明 ctrl+shift+N ctrl+shift+R 通过文件名快速查找工程内的文件(必记) ctrl+shift+alt+N ctrl+s ...
- windows下,java环境变量的设置,设置点击startup.bat启动tomcat
1.首先.安装好java jdk以后环境变量设置: CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar JAVA_HOME:C:\ ...
- 使用MVC5的Entity Framework 6入门 ---- 系列教程
使用MVC5的Entity Framework 6入门(十二)——为ASP.NET MVC应用程序使用高级功能 为ASP.NET MVC应用程序使用高级功能这是微软官方教程Getting Starte ...
- c++从string类型转换为bool类型
利用输入字符串流istringstream bool b; string s="true"; istringstream(s)>>boolalpha>>b; ...
- 嵌入式Linux软件工程师面试题一
题一,同步和异步有啥区别? 题二,TCP与UDP有啥区别? 题三,进程和线程有啥区别? 题一答案: 同步(Sync):所有的操作都做完,才返回给用户.这样用户在线等待的时间太长,给用户一种卡死了的感觉 ...
- Tomcat 8.5 无法进入Manage APP
解决的方法 1. 添加 Context http://stackoverflow.com/questions/36703856/access-tomcat-manager-app-from-diffe ...
- [转]csharp:Microsoft.Ink 手写识别(HandWriting Recognition)
原贴:http://www.cnblogs.com/geovindu/p/3702427.html 下載: //Microsoft Windows XP Tablet PC Edition 2005 ...