【题目】

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

N个加油站一个圈,加油gas,耗费cost。

要求选一个加油站出发,可以开车经过所有加油站。

数据特点:具有唯一解,全正数,非空,等长。

【思路】

基于以下两个很机智的结论

1、若第i个加油站,gas[i]-cost[i]<0,必不能从这里出发。

2、因为加油站是一个圈,若ΣΔ(gas[i]-cost[i])<0无解。

【代码】

class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int tank=0;//当前站出发后Δ装油量
int start=0;//出发站点
int total=0;//循环总Δ油量
for(int i=0;i<gas.length;i++){
int delta=gas[i]-cost[i];
tank+=delta;
total+=delta;
if(tank<0){
tank=0;
start=i+1;
//站点i出发为负,必然只能从下一站点出发
}
}
return total<0?-1:start;
}
}

【举例】

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.

[Leetcode 134]汽车加油站 Gas Station (环形)的更多相关文章

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

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

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

  4. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

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

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

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

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

  7. [leetcode greedy]134. 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] 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 ...

随机推荐

  1. 用 MoveTowards实现多点移动

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveCube ...

  2. 6.MongoDB4.0在Windows环境的下载、安装、配置

    简单来说:MongoDB4.0在Windows下已经不需要再次配置db文件夹之类操作,安装完成直接进行连接测试即可,以下是具体过程(此前网上很多的教程都已经过时) 1.下载:https://www.m ...

  3. Web_Toy

    1 2 3 4 1.App录音 var r = plus.audio.getRecorder() # 创建录音对象 r.record({filename:"_doc/audio/" ...

  4. 1005 继续(3n+1)猜想 (25 分)

    1005 继续(3n+1)猜想 (25 分)   卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推 ...

  5. nodejs笔记之搭建服务器

    简单服务器搭建: 1.新建一个文件:app.js 2.加入实现服务器代码: const http = require("http"); http.createServer(func ...

  6. PHP保留两位小数并且四舍五入及不四舍五入的方法

    php保留两位小数并且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); php保留两位小数并且不四舍五入 $num = ...

  7. nginx-编译安装 第一章

    nginx 第一章:编译安装 nginx 官网网站:http://nginx.org/en/ 1.基础说明 基本HTTP服务器功能其他HTTP服务器功能邮件代理服务器功能TCP/UDP代理服务器功能体 ...

  8. kvm键盘使用

    在新建导向的时候最后一步之前,选择查看细节那里,在desplay的地方选择VNC server ,再在keyboard地方选择us-en,这下进入安装界面就可以了.

  9. leecode第一百六十九题(求众数)

    class Solution { public: void quick_sort(vector<int>& nums,int res,int res_end) { )//错过,不能 ...

  10. Asp.net core (学习笔记 路由和语言 route & language)

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1 https://doc ...