[LeetCode 题解]:Gas Station
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
2. 思路
题解: 与经典的最大字段和问题类似,采用贪心策略。
3. 解法
1 class Solution {
2 public:
3 int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
4 int station = gas.size();
5 int i,sum=0,start=0,total=0;
6
7 for(i=0;i<station;i++)
8 gas[i]-=cost[i];
9
10 for(i=0;i<station;i++) total+=gas[i];
11 if(total<0) return -1;
12
13 // 与求最大子段和类似,使用贪心策略
14 for(i=0;i<station;i++)
15 {
16 sum+= gas[i];
17 if(sum<0)
18 {
19 start=i+1; // 如果当前和为负数,选择下一个元素作为起点
20 sum=0;
21 }
22 }
23 return start;
24 }
25 };
4. 相关题目
Maximum Subarray : http://www.cnblogs.com/double-win/p/3746672.html
![]() |
作者:Double_Win 出处: http://www.cnblogs.com/double-win/p/3746637.html 声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~ |
[LeetCode 题解]:Gas Station的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [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 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 134. Gas Station ----- java
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode OJ] 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 ...
- [leetcode]134. Gas Station加油站
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...
- Java for 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 ...
随机推荐
- 03_java之基本语法
01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scanner类是属于引用数据类型,我们先了解下引用数据类型. * b: 引用数据类型的定义格式 * 与定义基本数据类型变 ...
- sql之分段统计
sql之分段统计 需求:获取一个县所有家庭人数在1-2人,3-4人,5-6人,6人以上的家庭数的数组 思路:通过CASE WHEN 将 CBFCYSL分组,然后统计数据条数. 语句: SELECT T ...
- VS2017更新后 在WIN7上找不到 stdio.h等的问题
项目->属性->配置属性->常规->windows SDK版本.将其换成你现在的版本即可解决问题,如果不行就重新下个最新版SDK,如WIN10的.
- 浅谈Job&JobDetail
JobDetai重要属性:
- Java如何快速修改Jar包里的文件内容
需求背景:写了一个实时读取日志文件以及监控的小程序,打包成了Jar包可执行文件,通过我们的web主系统上传到各个服务器,然后调用ssh命令执行.每次上传前都要通过解压缩软件修改或者替换里面的配置文件, ...
- cdoj525-猴子选大王 (约瑟夫环)
http://acm.uestc.edu.cn/#/problem/show/525 猴子选大王 Time Limit: 3000/1000MS (Java/Others) Memory Li ...
- Opencv 分水岭分割图片
#include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...
- opennebula 添加kvm主机日志
Sun Sep :: [ReM][D]: Req: UID: HostDelete invoked, Sun Sep :: [ReM][D]: Req: UID: HostDelete result ...
- ReactNative常用第三方控件
Flex可视化在线工具 http://the-echoplex.net/flexyboxes/?fixed-height=on&legacy=on&display=flex&f ...
- 修改apache的默认访问路径
