前言

 

【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网络监视器

    vnstat iftop nethogs dig ipcalc

  2. from 组件

    知识补充 :  location.href="/index/"                                      加路径或者网址都可以 location.h ...

  3. Redis持久化RDB和AOF原理

    前言: redis持久化方式分为两种:RDB快照和AOF方式(默认为RDB模式),当Redis服务器重启的时候,会自动恢复数据,优先从AOF中恢复,其次才从RDB中恢复   一.RDB快照模式 RDB ...

  4. jaegeropentracing的Java-client完整分布式追踪链

    jaegeropentracing的Java-client完整分布式追踪链,在分布式系统中透传trace信息 之前文章记录了jaegeropentracing的Java-client追踪链在单系统中的 ...

  5. ZooKeeper 学习笔记(一)

    第一章 ZooKeeper的基本概念 一.介绍 在过去,每个应用一般都是在单个机子(单处理器)上运行,现在这一状况已经发生了巨大的变化.在大数据和云计算的世界里,应用程序已经被分成多个独立的模块在不同 ...

  6. 服务器报警邮件发送到QQ邮箱,但是被系统拦截

    # 为啥发送到QQ邮箱呢?因为QQ邮箱可以和微信关联,第一时间收到消息 if 没有设置白名单,然后被拦截当做垃圾邮件了: 设置白名单就可以了,这样的状态特征是: 邮件在垃圾箱里面能找到 elif 还是 ...

  7. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 12—Support Vector Machines 支持向量机

    Lecture 12 支持向量机 Support Vector Machines 12.1 优化目标 Optimization Objective 支持向量机(Support Vector Machi ...

  8. 用Box2d物理引擎设计类似愤怒小鸟投篮游戏 物理引擎的引入和基本框架搭建

  9. 文件后缀与mime类型对应表

    //文档文件类型的 .ai application/postscript    .eps application/postscript    .exe application/octet-stream ...

  10. 解决:EXCEL复制粘贴,精度丢失

    公司一部分数据是存在elasticsearch里面的,但里面的ID设计得特别长,我是打算把ID号考出来,用jmeter批量 删除的,但复制粘贴到excel里,ID就会精度丢失. 后来找到一个办法,解决 ...