[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 ...
随机推荐
- 跟着太白老师学python day11 闭包 及在爬虫中的基本使用
闭包的基本概念: 闭包 内层函数对外层函数的变量(不包括全局变量)的引用,并返回,这样就形成了闭包 闭包的作用:当程序执行时,遇到了函数执行,它会在内存中开辟一个空间,如果这个函数内部形成了闭包, 那 ...
- DirectShow的RTP发包(H264)Filter <转>
转帖地址:http://blog.csdn.net/fan2273/article/details/77653700 DirectShow的RTP发包(H264)Filter 基于DirectShow ...
- 1.Hadoop集群搭建之Linux主机环境准备
Hadoop集群搭建之Linux主机环境 创建虚拟机包含1个主节点master,2个从节点slave1,slave2 虚拟机网络连接模式为host-only(非虚拟机环境可跳过) 集群规划如下表: 主 ...
- SQL 组内排序
SELECT t_time, code, name, CL, row_number () OVER (partition BY t_time ORDER BY cl) AS 组内排名1, --T_ti ...
- Linux实战教学笔记36:PHP服务缓存加速深度优化实践
一,PHP缓存加速器介绍与环境准备 1.1 PHP缓存加速器介绍 1.1.1 操作码介绍及缓存原理 当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码(Ope ...
- js是函数式的面向对象编程语言
js是函数式的面向对象编程语言,而非类式的面向对象编程语言
- Slim安装以及使用【转】
最近在用backbone.js 做东西,因为牵扯到REST services 所以需要后台支持,此处选择了php.Slim 是php的一个框架. 貌似国内文章对此的介绍比较少,在安装Slim的过程中出 ...
- 10 华电内部文档搜索系统 search02
搜索项目并不是一个很大的项目,在实际项目中往往是作为子项目和别的项目集成在一起的.比如说和OA项目集成在一起,作为另外一个项目的子系统来使用.搜索项目的功能并不复杂. 整个项目是文档搜索项目,如题:企 ...
- cannot launch node of type [arbotix_python/arbotix_driver]: arbotix_python
这个时候提示错误: ERROR: cannot launch node of type [arbotix_python/arbotix_driver]: arbotix_python ROS path ...
- [C++] Template Function _ Any number of parameters
Template Function _ Any number of parameters #include<iostream> #include<cstdarg> using ...