/**
* 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. CentOS7虚拟机安装VMware Tools

    1.在VMware中点击安装VMware Tools 2.挂载光驱 mount /dev/cdrom /mnt 3.拷贝解压,注意是大小写敏感的,如果不知道文件名可以用ls查看. [root@loca ...

  2. Do-Now—团队冲刺博客三

    Do-Now-团队 冲刺博客三 作者:仇夏 前言 不知不觉我们的项目已经做了三个多礼拜了,团队冲刺博客也写到了这第三篇,看着一个基本成型的APP安装在自己的手机上,一种喜悦感油然而生.好了,现在来看看 ...

  3. Linux了解知识点

    Linux知识点   1.linux系统内核最早由芬兰大学生linus Torvalds开发. 2.Linux主要用于服务器端和嵌入式两个领域. 3.Linux的特点:开放性.多用户.多任务.良好的用 ...

  4. Golang Go Go Go part2:变量及常量声明

    三.关键字及内置预声明常量.类型.函数 1.关键字 Go有25个关键字,只能用在语法允许的地方,不能作为名称使用,它们是: break          default         func    ...

  5. 【.NET Core微服务实战-统一身份认证】开篇及目录索引

    简介 ​ 学习.NETCORE也有1年多时间了,发现.NETCORE项目实战系列教程很少,都是介绍开源项目或基础教程,对于那些观望的朋友不能形成很好的学习思路,遇到问题怕无法得到解决而不敢再实际项目中 ...

  6. Android单元测试之三:使用模拟框架模拟依赖

    Android单元测试之三:使用模拟框架模拟依赖 基本描述 如果是一些工具类方法的测试,如计算两数之和的方法,本地 JVM 虚拟机就能提供足够的运行环境,但如果要测试的单元依赖了 Android 框架 ...

  7. 什么是shell和终端?

    目录 什么是shell? 什么是终端? 什么是shell? 当谈到命令时,我们实际上指的是shell.shell是一个接收由键盘输入的命令,并将其传递给操作系统来执行的程序.几乎所有的Linux发行版 ...

  8. [Swift]LeetCode11. 盛最多水的容器 | Container With Most Water

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  9. [Swift]LeetCode209. 长度最小的子数组 | Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

  10. [Swift]LeetCode840. 矩阵中的幻方 | Magic Squares In Grid

    A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, co ...