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.
public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int start = 0; int cur = 0; int total = 0; for (int i = 0; i < gas.length; i++) { cur += gas[i] - cost[i]; total += gas[i]-cost[i]; if (cur < 0) { start = i+1; cur = 0; } } if (total >= 0) { return start; } else { return -1; } } }
相关推荐
zoj 1861 Gas Station Numbers.md
WPF.GasStation.BLL.dll文件,很难找到的dll,各位在程序开发时可以用到
Gas Station Management System in Python using Django
2030年加油站的业务模式创新(Business Model Innovation GAS STATION 2030)(英文)-word资料.pdf
贪婪算法的一道题,加油站加油,对于掌握贪婪算法比较有用
基于加油站网络的电动汽车充换电站最优布局模型,陈楚月,华国伟,电动汽车是公认的环境友好型交通工具,电动汽车充换电站的建设对于推广电动汽车具有重要的意义,但是目前在许多城市的充换电站建
该系统为加油站管理系统,内含和数据库建立联系的代码,还有进、销、存、统计等模块功能
这道题,题目名字就叫gas-station(网址的最后一部分),于是此题目的代码也在gas-station.c文件中。 当一道题通过测试且性能达到预期时,将在git中commit,注释为“测试通过,性能达标”。若commit的注释为其它内容...
gas ...gas-station 动态规划 palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca
gas station leetcode 非官方顺序leetcode题解,主要代码为Python和C++。 leetcode 第1题: leetcode 第2题: leetcode 第3题: leetcode 第4题: leetcode 第5题: leetcode 第6题: leetcode 第7题: leetcode 第9...
gas station leetcode algorithm 记录一下学习算法的一些代码。 冗余连接 II 全排列 II 左叶子之和 子集 把二叉搜索树转换为累加树 监控二叉树 合并二叉树 二叉搜索树中的众数 从中序与后序遍历序列构造二叉树 路径...
gas station leetcode LeetCode- 坚持每天刷一道算法题,冲鸭!!! day1 验证回文字符串 day2 亲密字符串 柠檬水找零 day3 反转字符串中的单词 day4 三数之和 day5 数组中的第k个最大元素 day6 环形链表II day7 无...
加油站模型,智能自动化管理系统,加油站建设者,部分流程优化。
1.3. With low buildings the variety... The older New England villages have changed relatively little __________ a gas station or two in recent decades. A.except B.besides C.in addition to D.except for 答
加油站快车 面向运行完整节点的任何人的轻量级以太坊汽油价格Oracle 这是一个简单的汽油价格预告片,如果您正在运行本地geth或奇偶校验节点,则可以使用。 它将查看最近200个区块的天然气价格,并根据接受的最低天然...
logger.debug("Starting search for nearest gas station."); //命名为“com.foo.bar”的实例barlogger会继承实例“com.foo”的级别。因此,下面这个请求可用,因为INFO >= INFO barlogger.info(...
134_Gas_Station 118_Pascal's_Triangle_I 119_Pascal's_Triangle_II 169_Majority_Element 229_Majority_Element_II 274_H_索引 275_H_Index_II 217_Contain_Duplicate 55_Jump_Game 45_Jump_Game_II 121_Best_Time...
Case Study – A Simple Gas Station System
Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of Array Except Self Game of Life Increasing Triplet Subsequence 单链表 Reverse ...
加油站 leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths ...Produ