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) 解集中不能 ...
随机推荐
- [Perl] 删除数组中重复元素
写一个小程序时候,需要去除一个数组中的重复元素,搜索了一下,找到的代码主要是两种,一种是使用grep函数,一种是转换为hash表,代码分别如下: 使用grep函数代码片段:代码: my @array ...
- Linux高级文本处理命令
cut 一.cut命令 功能:cut命令可以从一个文本文件/文本流中提取文本列 语法: cut -d '分割字符' -f fields ##用于有特定分割字符 cut -c 字符区间 ##用于排列整齐 ...
- 如何彻底卸载Jenkins(Windows版本)
起因: 最近在做持续集成测试过程中遇到一个问题,之前部署的Jenkins管理员密码忘了之后无法登陆,而且删除掉tomcat下webapps文件夹中的Jenkins目录后,再次安装Jenkins后相关的 ...
- SOALog
项目地址 : https://github.com/kelin-xycs/SOALog SOALog 为 SOA 架构 提供一种 松耦合 乐观 的 数据一致性 解决方案,说白了这个组件的功能就是 记 ...
- junit 知识点
JUnit 测试框架具有以下重要特性: 测试工具 测试套件 测试运行器 测试分类 测试工具 测试工具是一整套固定的工具用于基线测试.测试工具的目的是为了确保测试能够在共享且固定的环境中运行,因此保证测 ...
- POJ1050最大子矩阵面积
题目:http://poj.org/problem?id=1050 自己用了n^4的像暴搜一样的方法,感到有点奇怪——真的是这样? #include<iostream> #include& ...
- Microsoft Dynamics CRM4.0 和 Microsoft Dynamics CRM 2011 JScript 方法对比
CRM 2011 如果需要再IE里面调试,可以按F12在前面加上contentIFrame,比如 contentIFrame.document.getElementById("字段" ...
- ExtJS中,将Grid表头中的全选复选框取消复选
今天发现公司产品用的EXTJS中使用Grid时,Grid表头中的全选复选框的选中状态不是很准确,就写了这个小扩展 在js中加入下面方法,在需要取消全选的地方调用即可,例:Ext.getCmp('gri ...
- android onSaveInstanceState()及其配对方法。
转自:http://blog.chinaunix.net/uid-22985736-id-2977672.html onSaveInstanceState() 和 onRestoreInstanceS ...
- Neo4j在Centos7下的安装笔记
首先,要在官网找到tar的安装包路径,然后使用wget来安装.下载之后解压. 然后运行 bin/neo4j start 就可以启动了. 启动之后防火墙开放7474,发现仍然访问不了. 因为这里和win ...