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.

贪心or动态规划?

从gas[0]开始走,遇到油不够的情况,开始station就后退一步,利用以前算的结果可以只算出第一步的油量。直到最后,算出circle的连接点是否满足。

public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) { int n = gas.length; int start = 0, end = 0, have = 0, cur = 0; for(int i=0; i<n-1; i++) {
have += gas[cur] - cost[cur];
if(have >= 0) {
end ++;
cur = end;
}
else {
start --;
if(start < 0) {
start = n - 1;
}
cur = start;
}
} have += gas[cur] - cost[cur]; return have>=0?start:-1;
}
}

LeetCode——Gas Station的更多相关文章

  1. [LeetCode] Gas Station 加油站问题

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

  2. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  3. [leetcode]Gas Station @ Python

    原题地址:https://oj.leetcode.com/problems/gas-station/ 题意: There are N gas stations along a circular rou ...

  4. LeetCode: Gas Station 解题报告

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

  5. [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

    LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...

  6. [Leetcode] gas station 气站

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

  7. [LeetCode] Gas Station 贪心

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

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

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

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

随机推荐

  1. 用C#表达式树优雅的计算24点

    思路:一共4个数字,共需要3个运算符,可以构造一个二叉树,没有子节点的节点的为值,有叶子节点的为运算符 例如数字{1, 2, 3, 4},其中一种解的二叉树形式如下所示: 因此可以遍历所有二叉树可能的 ...

  2. C++ 记事本: 变量

    C++ 变量也许和其他语言的变量没有什么差别.就是用来存储一些可能会变值的容器. 当然 C++ 变量里又分为 原子类型 的(int , char ,bool 等等),复合类型 的(struct ,cl ...

  3. js笔记--1

    1.创建一个layer层 var GameLayer = cc.Layer.extend({ _time:null, _ship:null, _backSky:null, // 构造函数 ctor:f ...

  4. android studio svn不显示问题

    今天更新android studio SDK 重启后发现SVN功能没有了,工具栏也没有了更新提交按钮,刚开始以后是SVN程序可以不行了(在我的电脑提交更新没问题)我在官网下了最新1.9.4 打开and ...

  5. python 中偏函数 partial 的使用

    函数的partial应用 函数在执行时,要带上所有必要的参数进行调用.但是,有时参数可以在函数被调用之前提前获知.这种情况下,一个函数有一个或多个参数预先就能用上,以便函数能用更少的参数进行调用. 例 ...

  6. ecshop 加广告出现广告位的宽度值必须在1到1024之间

    打开 admin/ad_position.php这个文件,搜索1024,这里你会搜到两个地方 在236行左右 if ($ad_width > 1024 || $ad_width < 1) ...

  7. 【jquery】jquery.cookie.js 的使用指南

    之前有写过一篇原生 js 的 cookie 介绍,并且最后封装成 cookie.js 具体内容点击传送门. jquery.cookie.js 是一款轻量级的 cookie 插件,可以读取,写入和删除 ...

  8. 用Hibernate Tools生成Hibernate Mapping映射文件

    Eclipse中要集成安装Hibernate Tools组件 如果没有,请查看:Eclipse juno 中安装 JBoss Tools,集成Hibernate 一.确定环境: 1.Maven3.0. ...

  9. LoadRunner 12试用

    LoadRunner 12试用 http://blog.csdn.net/testing_is_believing/article/details/23611845

  10. iOS项目立项

    哎,计划总是赶不上变化,仿佛又回到了十年前高三的时候,每月.每周.每天都有计划,但是每周.每天都有计划外的因素导致了计划时时变,唯一不变的就只有变化了. 想了许久,中期计划内还是转回iOS吧,说转回其 ...