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

比较简单的题目,只要想到一个策略就好了.(貌似是贪心?)

上代码了,其实主要思想就是看下当前的汽油够不够用.

public class Solution {

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int g=0;
        int c=0;
        int tg=0;
        int tc=0;
        int ret=0;
        boolean flag = false;
        for(int i=0;i<cost.length;i++){
            g+=gas[i];
            c+=cost[i];

            tg+=gas[i];
            tc+=cost[i];
            if(gas[i]>cost[i] && !flag){
                ret = i;
                flag = true;
            }
            if(tg<tc){
                tg=0;
                tc=0;
                flag = false;
            }
        }
        if(c<=g) return ret;
        return -1;
    }

    public static void main(String[] args) {
      Solution s = new Solution();
      int[] gas = new int[]{2};
      int[] cost = new int[]{2};
      System.out.println(s.canCompleteCircuit(gas, cost));
    }
}
时间: 2024-08-08 05:22:05

[LeetCode]Gas Station的相关文章

Leetcode:Gas Station 加油站

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 journ

[leetcode]Gas Station @ Python

原题地址:https://oj.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

[LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 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

LeetCode——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

LeetCode: Gas Station [134]

[题目] 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

LeetCode &quot;Gas Station&quot; - TO BE REVISED

First I thought of a DP solution.. but TLE. So there must be a O(n). I thought of Mono-Queue.. but looks too complex. So what is the naive solution? O(n^2) of course. If we pick next start station smartly, we can make it linear: http://blog.csdn.net/

LeetCode: Gas Station 解题报告

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 journ

[LeetCode] 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

[LeetCode]Gas Station 最大子序列和,贪心

在一个环形路径上分布着n个加油站,从一个加油站到下一个加油站会耗油.要找出一个起始点,从这个起始点出发,存油量>=耗油量.解保证唯一. 这要求在路径上不能出现非负的. 如果找出存油量最大的子序列,则可确保尽可能走完全程(贪心思想). 因此,需要在环形数组中找一个最大子序列和.最大子序列和是很经典的DP问题,而针对环形约束,采用的是将数组复制一份并放到原数组最后,然后采用普通的最大子序列求解.其间,需要记录起始位置(这是最终要求的),走过的站点数(站点数不能超过n,这是环形数组,长度为2n),最大