• leetcode贪心134


    
    /**
     * <p>在一条环路上有 <code>n</code>&nbsp;个加油站,其中第 <code>i</code>&nbsp;个加油站有汽油&nbsp;<code>gas[i]</code><em>&nbsp;</em>升。</p>
     *
     * <p>你有一辆油箱容量无限的的汽车,从第<em> </em><code>i</code><em> </em>个加油站开往第<em> </em><code>i+1</code><em>&nbsp;</em>个加油站需要消耗汽油&nbsp;<code>cost[i]</code><em>&nbsp;</em>升。你从其中的一个加油站出发,开始时油箱为空。</p>
     *
     * <p>给定两个整数数组 <code>gas</code> 和 <code>cost</code> ,如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 <code>-1</code> 。如果存在解,则 <strong>保证</strong> 它是 <strong>唯一</strong> 的。</p>
     *
     * <p>&nbsp;</p>
     *
     * <p><strong>示例&nbsp;1:</strong></p>
     *
     * <pre>
     * <strong>输入:</strong> gas = [1,2,3,4,5], cost = [3,4,5,1,2]
     * <strong>输出:</strong> 3
     * <strong>解释:
     * </strong>从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
     * 开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
     * 开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
     * 开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
     * 开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
     * 开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
     * 因此,3 可为起始索引。</pre>
     *
     * <p><strong>示例 2:</strong></p>
     *
     * <pre>
     * <strong>输入:</strong> gas = [2,3,4], cost = [3,4,3]
     * <strong>输出:</strong> -1
     * <strong>解释:
     * </strong>你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
     * 我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
     * 开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
     * 开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
     * 你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
     * 因此,无论怎样,你都不可能绕环路行驶一周。</pre>
     *
     * <p>&nbsp;</p>
     *
     * <p><strong>提示:</strong></p>
     *
     * <ul>
     * <li><code>gas.length == n</code></li>
     * <li><code>cost.length == n</code></li>
     * <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
     * <li><code>0 &lt;= gas[i], cost[i] &lt;= 10<sup>4</sup></code></li>
     * </ul>
     * <div><div>Related Topics</div><div><li>贪心</li><li>数组</li></div></div><br><div><li> 1005</li><li> 0</li></div>
     */
    
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        //如果 从1出发到4 剩下的油小于0,那么 2,3也无法走到。
        public int canCompleteCircuit(int[] gas, int[] cost) {
            int n = gas.length;
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += gas[i] - cost[i];
            }
            if (sum < 0) {
                return -1;
            }
    
            int tank = 0;
            int start = 0;
            for (int i = 0; i < n; i++) {
                tank += gas[i] - cost[i];
                if (tank < 0) {
                    tank = 0;
                    start = i + 1;
                }
            }
            return start == n ? 0 : start;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)
    
    
  • 相关阅读:
    UML中类图的符号解释
    Vim简明教程【CoolShell】
    C++ 指针—02 指针与引用的对照
    一个通用onReady函数的实现
    内存泄漏以及常见的解决方法
    个人博客之路
    WPF 设置WebBrowser控件不弹脚本错误提示框
    Solr使用入门指南
    用C语言写解释器(一)——我们的目标
    数据库索引的作用和长处缺点
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/16560899.html
Copyright © 2020-2023  润新知