思路:

https://leetcode.com/problems/gas-station/discuss/269604/Java-Greedy-thought-process

关键是要想清楚如果从加油站A出发到不了B,那么从A到B之间的任何一个加油站出发也到不了B。

实现:

 #include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost)
{
int n = gas.size(), sum = , start = , ans = -;
for (int i = ; i < * n; i++)
{
sum += gas[i % n];
sum -= cost[i % n];
if (sum < ) { sum = ; start = i + ; }
if (i - start == n) { ans = start; break; }
}
return ans;
}
};
int main()
{
// int g[] = {1, 2, 3, 4, 5};
// int c[] = {3, 4, 5, 1, 2};
// int g[] = {2, 3, 4};
// int c[] = {3, 4, 3};
int g[] = {, , , , };
int c[] = {, , , , };
vector<int> gas(begin(g), end(g));
vector<int> cost(begin(c), end(c));
cout << Solution().canCompleteCircuit(gas, cost) << endl;
return ;
}

leetcode134 Gas Station的更多相关文章

  1. Leetcode134. Gas Station加油站

    在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...

  2. [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 ...

  3. LeetCode134:Gas Station

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

  4. [LeetCode] Gas Station 加油站问题

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

  5. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  6. Leetcode 134 Gas Station

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

  7. 【leetcode】Gas Station

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

  8. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  9. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

随机推荐

  1. 一些常用的页面js收集

    //正则表达式 验证整数格式function checkInt(tint){ var re=/^[-]{0,1}[1-9]+[0-9]*]*$/; //判断字符串是否为数字 if (re.test(t ...

  2. Microsoft visual studio关闭安全检查

    在用Microsoft visual studio进行代码编写时,使用到列如sprintlf这种比较旧的指令,需要关闭Microsoft visual studio的安全检查: 设置预处理选项:a. ...

  3. JAVA 内部类 (二)

    一.为什么要使用内部类 为什么要使用内部类?在<Think in java>中有这样一句话:使用内部类最吸引人的原因是:每个内部类都能独立地继承一个(接口的)实现,所以无论外围类是否已经继 ...

  4. Owin asp.net 脱离 IIS

    http://www.cnblogs.com/jesse2013/p/owin-webserver.html

  5. cx_Oracle库导入失败引起crontab中python程序运行失败,并且无错误提示

    今天遇到一个问题: 一个python脚本命令行运行时很正常,放到crontab中就无法工作,日志也没有记录,找了半天,终于发现问题所在. 在脚本最上方,程序如下: #!/usr/local/bin p ...

  6. cf 424

    Office Keys 首先显然有随人位置的递增,钥匙的位置也要递增,这样考虑两张做法: 1.$f(i,j)$ 表示前i个人,钥匙到第j个最少用的最大时间,然后$O(nK)$ dp 2.二分时间,对于 ...

  7. Linux系统如何查看版本信息?

    查看版本号 我在Ubuntu下做测试 1 命令行执行 cat /etc/issue  (切记cat后要空一格)即可看到版本信息. 2 登录linux,在终端输入 cat /proc/version  ...

  8. 二Java的常量与变量-1-1标识符

    类的名字就是标识符 起类名也是不能带空格的

  9. mysql 三存储引擎

    一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型 ...

  10. python3 类 组合

    解决类与类之间代码冗余问题有两种解决方案: 第一 是继承,第二是组合 1:继承   描述的是类与类之间的也就是什么是什么的关系 2: 组合  描述的是类与类之间的关系,  是一种什么有什么的关系的,也 ...