2019-06-01 17:09:30

问题描述:

问题求解:

其实本题本质上是一个数学题。

【定理】

对于一个循环数组,如果这个数组整体和 SUM >= 0,那么必然可以在数组中找到这么一个元素:从这个数组元素出发,绕数组一圈,能保证累加和一直是出于非负状态。

【证明】

从第一个数字开始进行累加和,中间必然会有一个累加和最低的点,我们设为x。最后的sum >= 0。

现在我们从x出发向后累加,那么必然处于非负状态,并且到了最后一个站点还会有一定的盈余,再从开始向最低点x进发也必然不会出现负数的情况。

【Leetcode Discuss】

If sum of all gas[i]-cost[i] is greater than or equal to 0, then there is a start position you can travel the whole circle.
Let i be the index such that the the partial sum

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]

is the smallest, then the start position should be start=i+1 ( start=0 if i=n-1). Consider any other partial sum, for example,

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]+gas[i+1]-cost[i+1]

Since gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i] is the smallest, we must have

gas[i+1]-cost[i+1]>=0

in order for gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]+gas[i+1]-cost[i+1] to be greater.
The same reasoning gives that

 gas[i+1]-cost[i+1]>=0
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]>=0
.......
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]+...+gas[n-1]-cost[n-1]>=0
 

What about for the partial sums that wraps around?

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[j]-cost[j] + gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1]
>=
gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i] + gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1]
>=0

The last inequality is due to the assumption that the entire sum of gas[k]-cost[k] is greater than or equal to 0.
So we have that all the partial sums

gas[i+1]-cost[i+1]>=0,
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]>=0,
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]+...+gas[n-1]-cost[n-1]>=0,
...
gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1] + gas[0]-cost[0]+gas[1]-cost[1]+...+gas[j]-cost[j]>=0,
...

Thus i+1 is the position to start.

因此,对于本题来说,我们可以计算一下是否总的gas - cost >= 0?如果是,那么必然存在一个解,在这个前提下,只需要遍历一遍数组,如果碰到不能到达的情况,那么就从第一个不能到达的重新开始计算即可。

    public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
int sum = 0;
for (int i = 0; i < n; i++) sum += gas[i] - cost[i];
if (sum < 0) return -1;
int start = 0;
int tank = 0;
for (int i = 0; i < n; i++) {
tank += gas[i];
if (tank < cost[i]) {
start = i + 1;
tank = 0;
}
else {
tank -= cost[i];
}
}
return start;
}

  

加油站问题 Gas Station的更多相关文章

  1. LeetCode 134. 加油站(Gas Station)

    题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...

  2. [Swift]LeetCode134. 加油站 | Gas Station

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

  3. [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

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

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

  6. LeetCode OJ:Gas Station(加油站问题)

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

  7. 力扣——gas station (加油站) python实现

    题目描述: 中文: 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[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. 134. Gas Station加油站

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

随机推荐

  1. 数据大爆炸:KDD 2016

    2016"> 饕餮盛宴 ACM SIGKDD国际会议(简称KDD)是数据挖掘领域的顶级国际会议,由ACM (计算机协会)的数据挖掘及知识发现专委会(SIGKDD)负责组织筹办.在现今如 ...

  2. 使用纯粹的JS构建 Web Component

    原文链接:https://ayushgp.github.io/htm...译者:阿里云 - 也树 Web Component 出现有一阵子了. Google 费了很大力气去推动它更广泛的应用,但是除 ...

  3. Layabox 预制体prefab使用

    //腊鸭官方api不详细系列之ui预制体 // 创建预制体文件,随便拖一个场景中的预制体到 Assets的任意文件夹中,要规范的话则放在Prefab中 // 上一步操作完后就可以在文件夹中看到.pre ...

  4. 基于springcloud搭建项目-Feign篇(四)

    上一篇已经写过ribbon客户端负载均衡的用法了,这篇主要是介绍feign的用法,首先我们必须了解feign是什么?能干嘛?怎么用? 这里简单介绍一下,然后进行代码测试 1.概述 Feign是一个声明 ...

  5. 仿IntelliJ Darcula的Swing主题FlatLaf使用方法

    最近Sandeepin想写个基于Java Swing的RSS阅读器练练手,不过Swing默认主题太丑了,切成系统原生的主题也不是非常好看,正好感觉开发时用的IDEA主题很不错,不管是Light还是Da ...

  6. 《JavaScript 模式》读书笔记(2)— 基本技巧1

    这篇文章的主要内容,介绍了一些js编程中的基本技巧,其实这些技巧,大家在开发的过程中,或多或少都在使用,或者已经可以熟练的应用于自己的代码或项目中了.那么,这篇文章,就一起来回顾下这些“基本技巧”. ...

  7. SpringFactoriesLoader解析

    一.SpringFactoriesLoader 介绍 1.1 SpringFactoriesLoader 简介 SpringFactoriesLoader 工厂加载机制是 Spring 内部提供的一个 ...

  8. Linux系统是什么?亲身自学经历分享

    我是数字媒体专业学生,第一次接触LINUX的时候,是大一C语言课程里看到的,书上讲了C语言的发展历史.说到C语言的起源,就离不开UNIX系统.在20世纪60年代,贝尔实验室的研究员Ken Thomps ...

  9. const 详解

    ​ 简单分类:          常变量        const 类型 变量名  或者   类型 const  变量名          常引用        const 类型& 引用名   ...

  10. tersserorc的简单使用

    tesserocr 是 python 的一个 OCR 库,它是对 tesseract 做的一层 Python API 封装,所以他的核心是tesseract. tesseract 的安装见 https ...