Leetcode_134_Gas_Station_Medium

Leetcode_134_Gas_Station_Medium

目录

Leetcode_134_Gas_Station_Medium

1. Leetcode_134_Gas_Station_Medium

1. 问题描述

2. 思路分析

3. Java代码

 


1. Leetcode_134_Gas_Station_Medium

1. 问题描述

/**************Leetcode_134_Gas_Station_Medium*********************/
    /**
     * Leetcode_134_Gas_Station_Medium
     * 难度:Medium
     * 题目介绍:
     * 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.
     * <p>
     * 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.
     * 给出两组数,第一组gas分别表示每个加油站可加的油,第二组cost分别表示
     * 目前加油站到达下一加油站所消耗的油量。要求从某一加油站加满油出发,
     * 顺时针循环所有的加油站并回到出发的加油站,返回该出发加油站的索引。
     * 如果中途油不够无法完成循环,则返回-1。
     * 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.
     * <p>

2. 思路分析

* 思路分析:
     * 注意:既然指定了gas staticn的坐标,因此,相当于只能顺时针旋转了。
     * 方法1:O(N^2).
     * 遍历一遍:从每一个gas station作为起点,当tank油量少于0时,就表示失败。
     * 首先过滤起点:保证起点gas[i]-cost[i]大于零才可以作为起点。
     * 然后通过for循环判断是否可以顺时针转一圈。
     * 方法2:O(N).
     * https://leetcode.com/problems/gas-station/discuss/294520/2-solutions-in-Java
     * https://blog.csdn.net/monkeyduck/article/details/51534795
     * 基于两条规律:
     * 1. 加油站的总油量小于总消耗时,肯定不能顺利跑一圈,返回-1;除此之外,肯定存在一个起点,可以顺利跑一圈。
     * 2. 当从i到j遇到车油量小于0时,表明从i到j中任意一点作为起点都不能顺利跑一圈;
     * (从点i开始,经过若干站后不能到达点j(j为i开始第一个不能到达的点),那么从点i到点j之间的任意一点开始都不可能到达点j。)
     * 因为:假设i与j之间任选一点A,从i到j最差情况下,车油量为0(能继续跑),车油量为0与从下一点作为起点是等价的;
     * i在最差情况下都不能到j,那么从A开始当然不能到j了。(因为油量为0为最差情况,一般情况不是最差的)
     *
     * 基于上述两个原则,就可以以O(N)的时间复杂度解决该问题。
     * 算法步骤:
     * 假设从第i个加油站开始加油并前进,当到达第j个油站油量小于0,说明i-j之间任何一点都不可以
     * 作为起点,继续从j+1开始作为起点,直至最后一个起点。

3. Java代码

/**
     * 方法1:暴力判断。
     */
    public int canCompleteCircuit_1(int[] gas, int[] cost) {
        if (gas == null || cost == null || gas.length == 0 || cost.length == 0) return -1;
        int stationSum = gas.length;
        for (int start = 0; start < stationSum; start++) {
            while (start < stationSum && gas[start] < cost[start]) start++;//过滤掉不合格的起点
            if (start == stationSum) return -1;//防止不存在合适起点;或者上一句while循环中<stationSum-1,留一个活口
            int currGas = 0;
            for (int currStation = start; currStation < stationSum + start; currStation++) {
                currGas += gas[currStation % stationSum] - cost[currStation % stationSum];
                if (currGas < 0) break;
            }
            if (currGas >= 0) return start;
        }
        return -1;
    }

    /**
     * 方法2:O(N);该方法比较妙。
     */
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas == null || cost == null || gas.length == 0 || cost.length == 0) return -1;
        int startGasStation = 0, currGas = 0, remindGas = 0;//currGas当前油量;remindGas总共油剩余量
        for (int i = 0; i < gas.length; i++) {//从第一个油站开始加油
            remindGas += gas[i] - cost[i];
            currGas += gas[i] - cost[i];
            if (currGas < 0) {//当前流量小于0
//                remindGas += currGas;
                currGas = 0;
                startGasStation = i + 1;
            }
        }
        return remindGas >= 0 ? startGasStation : -1;
    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值