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;
        }
    }
  • 相关阅读:
    LeetCode
    LeetCode
    一篇真正教会你开发移动端页面的文章(一)
    移动端页面开发资源总结
    Vue 模板
    使用 concurrently 并行地运行多个命令(同时跑前端和后端的服务)
    值得H5前端学习的60个JS插件(含DEMO演示)
    解读浮动闭合最佳方案:clearfix
    JavaScript 如何工作的: 事件循环和异步编程的崛起 + 5 个关于如何使用 async/await 编写更好的技巧
    JavaScript 运行机制详解:再谈Event Loop
  • 原文地址:https://www.cnblogs.com/airwindow/p/4235573.html
Copyright © 2011-2022 走看看