前言

 

【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的更多相关文章

  1. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  2. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  3. [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 ...

  4. 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 ...

  5. 【leetcode】Gas Station

    Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...

  6. 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 ...

  7. [LeetCode OJ] Gas Station

    问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...

  8. LeetCode _ Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. linux 远程连接服务器ftp命令整理

    Ftp命令的功能是在本地机和远程机之间传送文件.该命令的一般格式如下: $ ftp 主机名/IP ftp将给出提示符,等待用户输入命令: $ ftp ftp > 最常用的命令有: ls 列出远程 ...

  2. Consul 简介、安装、常用命令的使用

    1 Consul简介 Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发现的方案,Consul的方案更"一站式" ...

  3. 【Django】uWSGI和Gunicorn【转】

    因为nginx等优秀的开源项目,有不少本来不是做服务器的同学也可以写很多服务器端的程序了.但是在聊天中会发现,大家虽然写了不少代码,但是对wsgi是什么,gunicorn是什么,反向代理又是什么并不了 ...

  4. Javascript中prototype属性详解 (存)

    Javascript中prototype属性详解   在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不 ...

  5. Sunday

    字符串匹配中最简单的算法: 基本思路:wandderwonder wonder 建立256个长的next数组,记录每个字符最后一次出现的位置. 设mark1 = 0: 和 mark2 = 0:flag ...

  6. Vue+WebPack游戏设计:自动背景贴图和游戏主循环的实现

  7. cdoj525-猴子选大王 (约瑟夫环)

    http://acm.uestc.edu.cn/#/problem/show/525 猴子选大王 Time Limit: 3000/1000MS (Java/Others)     Memory Li ...

  8. NPC问题及其解决方法(回溯法、动态规划、贪心法、深度优先遍历)

    NP问题(Non-deterministic Polynomial ):多项式复杂程度的非确定性问题,这些问题无法根据公式直接地计算出来.比如,找大质数的问题(有没有一个公式,你一套公式,就可以一步步 ...

  9. Xbuild

    https://github.com/zhuayi/xbuild

  10. 微信小程序(一)基本知识初识别

    最近微信圈里小程序很火的样子,以前小程序刚开始的时候研究了一下,多日没关注发现一些东西都淡忘了,最后决定还是记录下来的好.    毕竟好记星比不上烂笔头嘛~