134. Gas Station
题目:
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.
链接: http://leetcode.com/problems/gas-station/
题解:
经典题gas station,贪婪法。设计一个局部当前的gas,一个全局的gas,当局部gas小于0时,局部gas置零,设置结果为当前index的下一个位置,此情况可能出现多次。当全局gas小于0的话说明没办法遍历所有gas站,返回-1。
Time Complexity - O(n), Space Complexity - O(1)。
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 0;
int curGas = 0;
int totalGas = 0;
int result = 0; for(int i = 0; i < gas.length; i++){
curGas += gas[i] - cost[i];
totalGas += gas[i] - cost[i];
if(curGas < 0){
result = i + 1;
curGas = 0;
}
} if(totalGas < 0)
return - 1; return result;
}
}
Update:
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if(gas == null || cost == null || gas.length != cost.length || gas.length == 0)
return -1;
int len = gas.length;
int totalGasRequired = 0, curGas = 0, station = 0; for(int i = 0; i < len; i++) {
totalGasRequired += gas[i] - cost[i];
curGas += gas[i] - cost[i];
if(curGas < 0) {
curGas = 0;
station = i + 1;
}
} return totalGasRequired >= 0 ? station : -1;
}
}
二刷:
Java:
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if (gas == null || cost == null || gas.length != cost.length) return -1;
int totalCost = 0, totalGas = 0, curTank = 0, startingIndex = 0; for (int i = 0; i < gas.length; i++) {
totalGas += gas[i];
totalCost += cost[i];
curTank += (gas[i] - cost[i]);
if (curTank < 0) {
curTank = 0;
startingIndex = i + 1;
}
}
return totalGas >= totalCost ? startingIndex : -1;
}
}
测试:
134. Gas Station的更多相关文章
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- Leetcode 134 Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- leetcode 134. Gas Station ----- java
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [LeetCode] 134. Gas Station 解题思路
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 【LeetCode】134.Gas Station
Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...
- 134. Gas Station加油站
[抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- 134. Gas Station(数学定理依赖题)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- 20160512--hibernate--缓存
缓存 缓存的作用主要用来提高性能,可以简单的理解成一个Map:使用缓存涉及到三个操作:把数据放入缓存.从缓存中获取数据.删除缓存中的无效数据. 原理模拟分析:(不能运行,只是模拟)(缓存实现复杂, ...
- linux添加用户、用户组、权限
# useradd –d /usr/sam -m sam 此命令创建了一个用户sam,其中-d和-m选项用来为登录名sam产生一个主目录/usr/sam(/usr为默认的用户主目录所在的父目录). 假 ...
- GetDeviceCaps() 参数
GetDeviceCaps 检测设备指定信息 参数: #define DRIVERVERSION 0 /* 设备驱动版本 */ #define TECHNOLOGY 2 /* Device class ...
- 禁用浏览器缓存Ajax请求
$.ajax({ url: 'url.php', cache: false, success: function(data){ //..... } }); 仅Get有缓存, Post不会缓存
- Strut2文件下载
Struts2控制文件下载,可以在文件下载之前做一些操作.这里就以权限控制为例,简单实现一下Struts2的文件下载. 一.Struts2文件下载的Action配置,是提供了一个能返回InputStr ...
- SpringInAction读书笔记--第4章面向切面
1.什么是面向切面编程 在软件开发中,散布于应用中多处的功能被称为横切关注点,这些横切关注点从概念上是与应用的业务逻辑相分离的,但往往分直接嵌入到应用的业务逻辑之中,把这些横切关注点与业务逻辑相分离正 ...
- C语言变参函数/Variadic fucntion
几个重要的 宏/类型 定义 Macros Defined in header <stdarg.h> va_start enables access to variadic function ...
- 暑假集训(2)第五弹 ----- Who's in the Middle(poj2388)
G - Who's in the Middle Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32 ...
- 数独的C++解法
grid.h #ifndef _GRID_H_ #define _GRID_H_ #include <set> #include <cstddef> class Grid { ...
- 使用webstorm上传代码到github
使用webstorm上传代码到github 字数681 阅读330 评论0 喜欢5 之前使用过webstorm上传代码到github,过了几个月竟然发现自己忘记了,好记性不如烂笔头啊,今天又重新用了一 ...