zoukankan      html  css  js  c++  java
  • [LeetCode#134]Gas Station

    The problem:

    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.

    My analysis:

    As the problem of detecting circle in a linked list, this problem also need a simple mathematical inference to get the right logic to fininish this problem! 
    The key idea: how could we preclude those nodes that could not be possible, without starting from each of those nodes(that's to say not to use brute force way).
    1. Once we start from a node, and we finally reach the termination when the total remained oil is negative.
    ==>we could preclude all nodes in the sequence, because : 
    s1, s2, s3, s4, ..., s7
    Suppose we start from s1, and when we reach s7, the remained oil is negative. 
    It means that at s1 ... s6 the remained oil is positive. If we start from s3, and since s2's remained oil is poistive, we would finally reach negative at s4, s5, s6 or s7. (since we have less oil remained in the tank, empty!!!)
    Thus, any station in the negative sequence is not possible.
    
    2. If the total remined oil is no less than zero, we must be able to find a station as answer. 
    Suppose when we scan from the first station and end at the last station, the original array could be divided into k negative sequences, and one positive sequences. Since the total remined oil is larger than zero: p1 + p2 + p3 ...+ pk + pa >= 0, we have pa > -p1 - p2 ... - pk. It means if we start from the starting node of pa, we must be able to back to pa with positive remained oil. 
    for (int i = 0; i < gas.length; i++) {
        total = total + gas[i] - cost[i];
        cur_sum =  cur_sum + gas[i] - cost[i];
        if (cur_sum < 0) {
            start = i + 1;
            cur_sum = 0;
        }
    }
    The code could be very elegant, no need to create any extra space for remained oil, casue once the current sequence is classfied as negative sequence, we could directly discard it!

    The solution:

    public class Solution {
        public int canCompleteCircuit(int[] gas, int[] cost) {
            if (gas == null || cost == null || gas.length == 0 || cost.length == 0 || gas.length != cost.length)
                return -1;
            int total = 0;
            int cur_sum = 0;
            int start = 0;
            for (int i = 0; i < gas.length; i++) {
                total = total + gas[i] - cost[i];
                cur_sum =  cur_sum + gas[i] - cost[i];
                if (cur_sum < 0) {
                    start = i + 1;
                    cur_sum = 0;
                }
            }
            if (total < 0)
                return -1;
            else
                return start;
        }
    }
  • 相关阅读:
    FlowNet2.0论文笔记
    LeetCode NO477.汉明距离总和
    自然语言的分词方法之N-gram语言模型
    C++函数模板及其实例化和具体化
    Vue2源码解读(5)
    Vue2源码解读(4)
    Vue2源码解读(3)
    Vue2源码解读(2)
    Vue2源码解读(1)
    vue的双向绑定原理及实现
  • 原文地址:https://www.cnblogs.com/airwindow/p/4235573.html
Copyright © 2011-2022 走看看