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 ...
随机推荐
- 将linux系统用户导入mysql表
下面这个程序实现的一个很简单的功能,读取passwd文件,将里面的用户信息写入到mysql里面, 具体代码如下: #!/usr/bin/python import pymysql import tim ...
- Linux终端程序用c语言实现改变输出的字的颜色
颜色代码: 格式: echo "\033[字背景颜色;字体颜色m字符串\033[0m" 例如: echo "\033[41;36m something here \033 ...
- linux 中的局部变量、全局变量、shell 变量的总结
系统局部变量和全局变量 一.变量分类局部变量和环境变量,局部变量只适用于当前shell,而环境变量是全局的,它适用于所有当前shell以及其派生出来的任意子进程,有些变量是用户创建的,其他的则是专用 ...
- JavaScript-Tool:jquery.zsign(电子签章)-un
ylbtech-JavaScript-Tool:jquery.zsign(电子签章) 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作 ...
- Eclipse如何查看接口实现类快捷键
1.找到要打开的接口类 2.双击接口名选中 3.按Ctrl+T打开接口实现类 以List接口为例,如下所示
- bat批处理教程
批处理的本质,是一堆DOS命令按一定顺序排列而形成的集合 1.ping sz.tencent.com > a.txt 把前面信息放到a.txt中 ping sz.tencent.com > ...
- 阻塞调用ShellExecute函数
SHELLEXECUTEINFO si;ZeroMemory(&si, sizeof(si));si.cbSize = sizeof(si);si.fMask = SEE_MASK_NOCLO ...
- Unity中HideInInspector和SerializeField
http://blog.sina.com.cn/s/blog_697b1b8c0102uxvn.html Unity会自动为Public变量做序列化,序列化的意思是说再次读取Unity时序列化的变量是 ...
- [Xcode 实际操作]七、文件与数据-(20)CoreML机器学习框架:检测和识别图片中的物体
目录:[Swift]Xcode实际操作 本文将演示机器学习框架的使用,实现对图片中物体的检测和识别. 首先访问苹果开发者网站关于机器学习的网址: https://developer.apple.com ...
- ubuntu 14.04 源码编译mysql-5.7.17
环境为 Ubuntu 12.04 64 位的桌面版 编译的mysql 版本为 5.7.18 首先需要安装一下依赖包 sudo apt-get install libncurses5-dev cmake ...