• [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;
        }
    }
  • 相关阅读:
    Visual Studio的输出窗口上输出调试信息的函数
    std::min error C2059: 语法错误:“::” 的解决方法
    error C2872: “flann”: 不明确的符号 --- PCL 与OpenCV2 的flann命名空间冲突问题的解决方法
    VS编译器中设置 输出窗口 只显示error,不显示warning 要如何配置
    nginx.conf的完整配置说明
    Nginx基本配置、性能优化指南
    Apache手册
    Apache 配置虚拟主机三种方式
    Linux常用命令汇总
    Linux下安装Apache
  • 原文地址:https://www.cnblogs.com/airwindow/p/4235573.html
Copyright © 2020-2023  润新知