/**
* Source : https://oj.leetcode.com/problems/gas-station/
*
* 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.
*/
public class GasStation { /**
*
* 判断车能否从某一个station开始绕所有的station走一圈
*
* sum(gas[i]-cost[i]) < 0表示一定不能走完,否则一定可以
*
* 如果能绕一圈,那么怎么寻找起点
*
* 性质1:如果从i出发可以绕一圈,那么从i出发可以达到任意station,显而易见
* 性质2:如果这样的i是唯一的,那么不存在 j!= i,从j出发能达到i,证明:使用反证法:假设这样的j存在,那么j能到达i,
* 由性质1得i可以到达任意station,那么i也可以到达j,那么就可以从j出发到达j,也就是说同时存在i、j可以绕一圈,与唯一解矛盾,所以不存在
* 性质3:假如i是唯一的解,那么从0至i-1出发无法到达i,从i出发可以到达i+1至n-1
*
* 结合以上三条性质,解法如下:
* 0:如果sum(gas[i]-cost[i]) < 0表示无解,否则有解,进入1
* 1:从0开始计算sum(gas[i]-cost[i]),如果sum < 0,则由0作为起点不能到达i,根据性质1,0不能作为起点,因为从0出发可以到达i,
* 由性质2,1至i-1不能作为起点,那么i+1作为新的候选起始点
* 3:以此类推,直到遇到k,从k出发能到达n-1,那么k就能绕一圈,因为这个时候k能到达n-1,加上sum(gas[i]-cost[i]) > 0一定有解的话,k就是起点
*
* @param gas
* @param cost
* @return
*/
public int canCOmpleteCircuit (int[] gas, int[] cost) {
int start = 0;
int sum = 0; // 从0开始的所有gas[i] - cost[i]和
int sumK = 0; // 从k开始的所有gas[i] - cost[i]和
for (int i = 0; i < gas.length; i++) {
sum += gas[i] - cost[i];
sumK += gas[i] - cost[i];
if (sumK < 0) {
start = i+1;
sumK = 0;
}
}
if (sum < 0) {
return -1;
}
return start;
}
}

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 is gas[i]. You ...

  7. [Leetcode] gas station 气站

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

  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]. You ...

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

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

随机推荐

  1. Android EventBus技能点梳理

    EventBus为Github上的开源项目,地址:https://github.com/greenrobot/EventBus 疑问:1. 现在都是Android Studio创建的项目,如何导入这些 ...

  2. sqlzoo:4

    列出每個國家的名字 name,當中人口 population 是高於俄羅斯'Russia'的人口. SELECT name FROM world WHERE population > (SELE ...

  3. Kiwi Syslog安装笔记

    Kiwi Syslog是一款比较好用日志服务器,采集设备.服务器日志简单又方便,近日试架成功,记录重点部分. 1. 网络设备配置 举例思科: R1#configure t R1(config)#log ...

  4. Java-IO 字节流的使用和效率比较

    打算做一个系列,前面讲了基本的字符流的用法,这篇博客介绍一下字节流的基本用法: 一.基本使用: 基本字节流: FileInputStream   FileOutputStream BufferedIn ...

  5. nginx获取上游真实IP(ngx_http_realip_module)

    realip模块的作用是:当本机的nginx处于一个反向代理的后端时获取到真实的用户IP,如果没有realip模块,nginx的access_log里记录的IP会是反向代理服务器的IP,PHP中$_S ...

  6. MVVM简介与运用

    在介绍MVVM框架之前,先给大家简单介绍一下MVC.MVP框架(由于本博文主要讲解MVVM,所以MVC和MVP将简化介绍,如果需要我将在以后的博文中补充进来). MVC框架: M-Model : 业务 ...

  7. centos7 mysql自动备份

    MySQL自动备份shell脚本   在数据库的日常维护工作中,除了保证业务的正常运行以外,就是要对数据库进行备份,以免造成数据库的丢失,从而给企业带来重大经济损失.通常备份可以按照备份时数据库状态分 ...

  8. Web测试入门:Selenium+Chrome+Python+Mac OS

    一.环境配置 Chromedriver 下载及环境配置 url:使用WebDriver在Chrome浏览器上进行测试时,需要从http://chromedriver.storage.googleapi ...

  9. Go语言反射reflect

    目录 通过反射获取类型信息 理解反射的类型(Type)与种类(Kind) reflect.Elem() - 通过反射获取指针指向的元素类型 通过反射获取结构体的成员类型 通过反射获取值信息 使用反射值 ...

  10. #Java学习之路——面试题

    (一)[基础知识梳理——JAVAse部分]Java中的变量和常量        在程序中存在大量的数据来代表程序的状态,其中有些数据在程序的运行过程中值会发生改变,有些数据在程序运行过程中值不能发生改 ...