zoukankan      html  css  js  c++  java
  • 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.

    Note:
    The solution is guaranteed to be unique. 

    Hide Tags
     Greedy 

    链接:  http://leetcode.com/problems/gas-station/

    6/8/2017

    110ms, 0.8%,丢人的运行时间

    想法是算出来remaining数组,然后从每个点出发开始算。

     1 public class Solution {
     2     public int canCompleteCircuit(int[] gas, int[] cost) {
     3         if (gas == null || cost == null || gas.length != cost.length) {
     4             return -1;
     5         }
     6         int[] remaining = new int[gas.length];
     7         for (int i = 0; i < remaining.length; i++) {
     8             remaining[i] = gas[i] - cost[i];
     9         }
    10         int i;
    11         int j = 0;
    12 
    13         for (i = 0; i < remaining.length; i++) {
    14             int sum = 0;
    15             if (remaining[i] < 0) {
    16                 continue;
    17             }
    18             for (j = 0; j < remaining.length; j++) {
    19                 int index = (i + j) % remaining.length;
    20                 sum += remaining[index];
    21                 if (sum < 0) {
    22                     break;
    23                 }
    24             }
    25             if (j == remaining.length) {
    26                 break;
    27             }
    28         }
    29         if (j == remaining.length) {
    30             return i;
    31         }
    32         return -1;
    33     }
    34 }

    别人的答案

    很巧妙的方法

    curGas用来计算当前的剩余是不是负的,是的话就重置curGas和startIndex。在整个过程中只要有负的,那么当时开始的startIndex就会被后面的赋值所覆盖。

    最后比较存活下来的startIndex,是否totalGas在走过整个数组之后仍然是非负的,若是,则证明这个startIndex不但能够保证其之后的gas station,也能保证包括其前面的那些gas station。否则没有一个startIndex可以覆盖所有Gas station,返回-1

     1 public class Solution {
     2     public int canCompleteCircuit(int[] gas, int[] cost) {
     3         if (gas == null || cost == null || gas.length != cost.length) {
     4             return -1;
     5         }
     6         int curGas = 0;
     7         int totalGas = 0;
     8         int startIndex = 0;
     9 
    10         for (int i = 0; i < gas.length; i++) {
    11             curGas += gas[i] - cost[i];
    12             totalGas += gas[i] - cost[i];
    13             if (curGas < 0) {
    14                 startIndex = i + 1;
    15                 curGas = 0;
    16             }
    17         }
    18         if (totalGas >= 0) {
    19             return startIndex;
    20         }
    21         return -1;
    22     }
    23 }

    类似更简单的

    https://discuss.leetcode.com/topic/1344/share-some-of-my-ideas

    这个看上去很厉害,但是没有看懂

    貌似是用2个指针,如果当前Sum非负,那么end从前往后走,若sum是负值, start从后往前尝试cover,最终等到start, end指向同一个位置时,判断最终的Sum是否非负

    https://discuss.leetcode.com/topic/5088/my-ac-is-o-1-space-o-n-running-time-solution-does-anybody-have-posted-this-solution

     1 class Solution {
     2 public:
     3     int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
     4 
     5        int start = gas.size()-1;
     6        int end = 0;
     7        int sum = gas[start] - cost[start];
     8        while (start > end) {
     9           if (sum >= 0) {
    10              sum += gas[end] - cost[end];
    11              ++end;
    12           }
    13           else {
    14              --start;
    15              sum += gas[start] - cost[start];
    16           }
    17        }
    18        return sum >= 0 ? start : -1;
    19     }
    20 };

    更多讨论

    https://discuss.leetcode.com/category/142/gas-station

  • 相关阅读:
    webpack入坑之旅(五)加载vue单文件组件
    webpack入坑之旅(四)扬帆起航
    webpack入坑之旅(三)webpack.config入门
    webpack入坑之旅(二)loader入门
    模块的总结
    项目中的bug
    详解懒汉模式和饿汉模式以及他们的改进
    感悟(岁月)
    浅谈js中的this的用法
    图解http协议(一章了解web及其网络基础h)
  • 原文地址:https://www.cnblogs.com/panini/p/6970089.html
Copyright © 2011-2022 走看看