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 ...
随机推荐
- Win32环境下代码注入与API钩子的实现
本文详细的介绍了在Visual Studio(以下简称VS)下实现API钩子的编程方法,阅读本文需要基础:有操作系统的基本知识(进程管理,内存管理),会在VS下编写和调试Win32应用程序和动态链接库 ...
- Thrift之代码生成器Compiler原理及源码详细解析2
我的新浪微博:http://weibo.com/freshairbrucewoo. 欢迎大家相互交流,共同提高技术. 2 t_generator类和t_generator_registry类 这个两 ...
- rsync应用实例
一. 通过ssh的方式 前面介绍的rsync 5种方式当中,第二.第三(1个冒号)就属于通过ssh的方式,这种方式其实就是让用户去登录到远程机器,然后执行rsync的任务. [root@local ...
- c语言编译器内置宏
注:转自http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html 感谢分享 前言: 我们在写程序的时候,总是 ...
- ASP.NET Core会议管理平台实战_2、基本概念的理解
id Token携带用户的信息 AccessToken:是否有权限访问资源 看数据库的表,Client相关的表,api的相关的表 Resources把用户的简介抽象出来到IdentityClaims表 ...
- Lua 5.1.1 源代码阅读笔记
http://blog.csdn.net/hamenny/article/details/4506130
- Linux whereis 搜索命令位置
在学习 兄弟连 linux教学视频 的时候,我将所学的 linux 命令记录在我的博客中,方便自己查阅. 文件搜索命令: whereis 基础的命令 命令名称:whereis 命令的所在路径:/usr ...
- Node.js学习(第四章:初见express)
Express框架是一款简洁而灵活的node.js web应用框架.前面我们自己手动创建服务器在Express中就是一个API的事情,这就使得我们更加注重业务的功能和开发效率上,不必纠结过多底层的事情 ...
- Exists 方法
public void ExistsMethodDemo() { string userId = "123"; string userName = "admin" ...
- 检测到"_ITERATOR_DEBUG_LEVEL"的不匹配项
error: vtkCommon.lib(vtkSmartPointerBase.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项:值“0”不 ...