134. Gas Station(js)

134. Gas Station

There are N gas stations along a circular route, where the amount of gas at station iis 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 in the clockwise direction, otherwise return -1.

Note:

  • If there exists a solution, it is guaranteed to be unique.
  • Both input arrays are non-empty and have the same length.
  • Each element in the input arrays is a non-negative integer.

Example 1:

Input:
gas  = [1,2,3,4,5]
cost = [3,4,5,1,2]

Output: 3

Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.

Example 2:

Input:
gas  = [2,3,4]
cost = [3,4,3]

Output: -1

Explanation:
You can‘t start at station 0 or 1, as there is not enough gas to travel to the next station.
Let‘s start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can‘t travel around the circuit once no matter where you start.题意:给定每个站的汽油补给量和花费量,问从哪个站开始出发可以环绕一周,若所有站都不满足则返回-1代码如下:
/**
 * @param {number[]} gas
 * @param {number[]} cost
 * @return {number}
 */
var canCompleteCircuit = function(gas, cost) {
    let sum=0,total=0,start=0;
    for(let i=0;i<gas.length;i++){
        sum+=gas[i]-cost[i];
        total+=gas[i]-cost[i];
        if(sum<0){
            sum=0;
            start=i+1;
        }
    }
    return total<0?-1:start;
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10946802.html

时间: 2024-08-25 11:06:33

134. Gas Station(js)的相关文章

百度静态资源(JS)公共库

     例如: chosen http://apps.bdimg.com/libs/chosen/1.1.0/chosen.jquery.min.js classlist http://apps.bdimg.com/libs/classlist/2014.01.31/classList.min.js cookies.js http://apps.bdimg.com/libs/Cookies.js/0.4.0/cookies.min.js dojo http://apps.bdimg.com/l

前台强大的图表(js)

http://www.jqplot.com/ 网址,自己下载下例子看一下就明白了. 这个是我之前做的项目中的例子(仅是部分代码): function publicMethod(){                         var plot1 = $.jqplot(chart, [currYear], {                 seriesColors: ["rgb(23, 108, 238)"],                 title: titles,     

创建HTML新元素(js)

1 <!-- 2 创建新的HTML元素 3 1.创建新的元素 4 2.创建新的节点 5 3.追加节点 6 4.向已有元素追加新的元素 7 --> 8 <html> 9 <body> 10 11 <div id="div1"> 12 <p id="p1">这是一个段落</p> 13 <p id="p2">这是另一个段落</p> 14 </div&g

134. Gas Station

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

给数组添加一个根据指定下标删除元素的方法、得到0-100的随机数不重复(js)、得到外联样式的css样式值

/** *删除数组指定下标或指定对象 */ Array.prototype.remove=function(obj){ for(var i =0;i <this.length;i++){ var temp = this[i]; if(!isNaN(obj)){ temp=i; } if(temp == obj){ for(var j = i;j <this.length;j++){ this[j]=this[j+1]; } this.length = this.length-1; } } }

[Lintcode]187. Gas Station/[Leetcode]134. Gas Station

187. Gas Station/134. Gas Station 本题难度: Medium Topic: Greedy Description 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

JavaScript(JS)之简单介绍

JavaScript(JS)之简单介绍 一.JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.(客户端执行的语言) Netscape(网景)接收Nombas的理念,(Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套livescript的脚本语言.Sun和Netscape共同完成.后改名叫Javascript 微软随后模仿在其IE3.0

JavaScript(JS)之Javascript对象

JavaScript(JS)之Javascript对象 简介: 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String.Math.Array.Date.RegExp都是JavaScript中重要的内置对象,在JavaScript程序大多数功能都是基于对象实现的 <script language="javascript"> var aa=Number.MAX_VALUE; //利用数字对象获取可

[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 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 e