亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

【LEETCODE】模擬面試-134-Gas Station

新生

题目:

https://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.


分析:

This question is to find the start point where the car can complete a circuit.

Input: two arrays, one represents the gas at each station point, another one is cost when the car travels from point i to point i+1.
**Output: **if the car can't finish a whole circuit, return -1, if yes, return the index of start point.
Note: there maybe multiple solutions, but we just need one path.

The brute force method is that we can scan from 0 to last, each point will be considered as the start once.
For each start, we will iterate all the following points in the circuit, and calculate the left gas.
If the gas decreases below 0 during the process, this start point is failing, so we can move to next start point.
If this start can complete the circuit, we can return its index, and no need to continue searching.

But how to optimize above method?
We can reduce some repeated work.
Take this data set as an example:

index:0,1,2,3,4,5,6
gas:3,4,3,6,7,1,2
cost:2,4,5,1,5,1,3

Since we only care about the left gas at each point, we can firstly transform two arrays to one:

leftGas:1,0,-2,5,2,0,-1

Suppose this, when we start from index=0, and we can move on till index=2,  since the sum_leftGas here is 1, but we can't move to index=3.
Then we can just ignore index=1,2 without considering them as start point, since they will be stuck at index_3 too.

And we continue from point index_3, it turns out all the left gas at each station can be >=0, so index_3 can be a start point.

This process is like a Queue:
since we scan from 0 to 3, and 0,1,2 fails to be a start, so we pop them out, and continue from 3 to 4,5,6,0,1,2, till 3, a circuit finished.

But we can still optimize it.
without pop 0,1,2 and add them again, we can just apply the structure Deque which can add and pop on both sides. Since the path will be a circuit, there's no need to delete any point, just add point on different sides, finally the points in de deque will combine a path.

So we start from 0, if the sum_gas will be >=0, we add next point on right side, otherwise, we add point on the left side.


Java

public class Solution {    public int canCompleteCircuit(int[] gas, int[] cost){        //corner
        if ( gas == null || cost == null || gas.length != cost.length )            return -1;        
        //core
        int start = gas.length - 1;                     //用two pointer模拟deque,并不需要建立deque
        int end = 0;        
        int gasLeftSum = gas[start] - cost[start];        
        while ( start > end ){                          
            
            if ( gasLeftSum >= 0 ){                         //end开始并为加进来,所以先加,再右移
                gasLeftSum += gas[end] - cost[end];
                end++;
            }else{
                start--;
                gasLeftSum += gas[start] - cost[start];     //start最开始已经被加上了,所以先左移,再加油
            }   
        }        
        return  gasLeftSum >= 0 ? start : -1;
    }
}


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消