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;
        }
    }
  • 相关阅读:
    叩开抽象的大门(2)——依赖于抽象
    威老迷宫探险第二季如何更面向对象
    更佳的封装之路面向对象的封装思想
    威老的迷宫探险
    重用,我要重用!!!
    威老出国记,什么是引用,别名。
    叩开抽象的大门(1)——抽象类、接口
    maven常用命令
    大公司喜欢问的问题
    java 发送http请求
  • 原文地址:https://www.cnblogs.com/airwindow/p/4235573.html
Copyright © 2011-2022 走看看