前言

 

【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 tcp调优

    Linux TCP Performance Tuning News Linux Performance Tuning Recommended Books Recommended Links Linux ...

  2. [Z]LaTeX入门教程

    LaTeX入门教程 Contents TEX/LATEX是什么? 为什么要用TEX/LATEX? 安装 开始使用 数学符号使用中文文章的各个部分表格 行内公式与行间公式 上标与下标 常见的数学公式 行 ...

  3. 系统键盘按钮keyCode大全

    字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C 67 L 76 U 85 3 ...

  4. leetcode413

    public class Solution { public int NumberOfArithmeticSlices(int[] A) { , sum = ; ; i < A.Length; ...

  5. 教你用一行Python代码实现并行(转)

    教你用一行Python代码实现并行 本文教你通过一行Python实现并行化. Python在程序并行化方面多少有些声名狼藉.撇开技术上的问题,例如线程的实现和GIL,我觉得错误的教学指导才是主要问题. ...

  6. T-SQL 之 执行顺序

    1.sql查询语句的处理步骤,代码清单 --查询组合字段 (5)select (5-2) distinct(5-3) top(<top_specification>)(5-1)<se ...

  7. Hash表从了解到深入(浅谈)

    · Hasn表,将一个数据进行Value化,再进行一个映射关系到Key直接进行访问的一个数据结构,这样可以通过直接的计算进行数据的访问和插入.关于Hash表的基本概念这里就不一一叙述,可以通过百度了解 ...

  8. C# 对象封装为json格式

    1 对象 public class Person { public string Name { get; set; } public int Age { get; set; } public Date ...

  9. Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务

    一.概念 分布式事务分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.简言之,同时操作多个数据库保持事务的统一,达到跨库事务的效果. JTA ...

  10. cloudstack-setup-databases cs数据安装

      cloudstack-setup-databases cloudstack-setup-databases user:[password]@mysqlhost:[port] [--deploy-a ...