leetcode134 Gas Station
思路:
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的更多相关文章
- Leetcode134. Gas Station加油站
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...
- [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]. ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 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 ...
- 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 ...
- 【leetcode】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- 20. Candy && Gas Station
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
随机推荐
- eclipse 切换数据库
PS:jdbc.url = 后面的路径就是连接的数据库,如果需要连接开发库(dev_cfin),则只需要将tg_cfin 换成 dev_cfin,重启后台即可.
- 数论 N是完全平方数 充分必要条件 N有奇数个约数
N是完全平方数 <----> N有奇数个约数 设:N = n*n 充分性: 1.N=1时,N的约数为1,为奇数 2.N>1时,1.....n......N,其中 1, n, N为N的 ...
- bzoj4455
容斥原理+dp 首先考虑暴力做法,我们希望点和点一对一,那么自然要保存当前点集的状态,需要状压,据说要3^n,那么自然不行 考虑容斥原理,刚才一一对应的限制太强了,我们不要一一对应,只要满足边存在就行 ...
- 深度技术Win7系统利用diskpart命令实现硬盘分区的技巧
转自:http://www.xitongcheng.com/jiaocheng/win7_article_2491.html 1. 深度技术Win7系统利用diskpart命令实现硬盘分区的技巧分享给 ...
- C#控件刷新
; ; foreach (string gen in fn_gen) { //MessageBox.Show(gen); Bitmap Bi = new Bitmap(gen); //使用打开的图片路 ...
- CF-832B
B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 单例模式-Lazy initialization holder class模式
这个模式综合使用了Java的类级内部类和多线程缺省同步锁的知识,很巧妙地同时实现了延迟加载和线程安全. 1.相应的基础知识 什么是类级内部类? 简单点说,类级内部类指的是,有static修饰的成员式内 ...
- java集合框架之Collection
参考http://how2j.cn/k/collection/collection-collection/366.html Collection是 Set List Queue和 Deque的接口Qu ...
- Linux下mysql启动失败
原因:强制重启服务器之后,发现mysql启动 失败 解决方法:强制清空 报错路径下的tmp文件,清空之后在tmp下面新建一个文件夹,文件夹的名字在你清空tmp之后启动mysql系统会给你提示 欧克,完 ...
- HDOJ1874最短路【spfa】
//不知道切过这道题目几次了,这次又wa了...双向路啊....这波简直无奈了,今晚又是浪成狗!!!! #include<cstdio> #include<vector> #i ...