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 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.
1、暴力的做法
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int len = gas.length;
int sum = 0;
int[] dp = new int[len];
for( int i = 0;i<len;i++){
dp[i] = gas[i]-cost[i];
sum+=dp[i];
}
int num = 0;
if( sum < 0)
return -1;
for( int i = 0;i<len;i++){
num = 0;
for( int j = i;j<len;j++){
num+=dp[j];
if( num < 0){
break;
}
}
if( num >= 0 )
return i;
}
return -1;
}
}
2、仔细看题目,发现答案其实是唯一解,那么就只需要从后向前遍历,找到最大子串和的起始位置i,(结束位置是最后一位)
在总消耗>0的前提下,返回起始位置i。
否则返回-1。
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int len = gas.length;
int sum = 0;
int max = 0;
int start = 0;
for( int i = len-1;i>=0;i--){
sum+=(gas[i]-cost[i]);
if( sum > max ){
max = sum;
start = i;
}
}
if( sum < 0)
return -1;
return start;
}
}
leetcode 134. Gas Station ----- java的更多相关文章
- Java for 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 (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
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加油站
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. Y ...
- [leetcode] 134. Gas Station (medium)
原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...
- 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】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
随机推荐
- C++-const_cast, reinterpret_cast, static_cast的用法
/////////////////////////////////////////////////////////////////////////////// // // FileName : cas ...
- web安全 -- 常见攻击方法及预防措施
一.sql注入 sql注入,是指攻击者在猜测出服务器上要执行sql后:通过输入数据,拼接原来要执行的sql而形成新的sql:从而到达改变原来查询的意义的目的. -- 原来sql select xxx ...
- fragment 添加menu
http://bbs.51cto.com/thread-1091458-1-1.html 有详解 @Override public void onCreate(Bundle savedInstance ...
- js 弹出div窗口 可移动 可关闭 (转)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- litepal的jar包
转自http://blog.csdn.net/luohai859/article/details/39292607 LitePal是一款开源的Android数据库框架,它采用了对象关系映射(ORM)的 ...
- libbspatch.so
http://www.zhihu.com/question/21154099 http://blog.csdn.net/hmg25/article/details/8100896 91助手和Googl ...
- C++ offsetof
这是一个宏,用于计算类中某个成员的地址相对于类实例的偏移量 在C++11中,要求这个类standard_layout 基本用法是这样子的: #include <stdio.h> /* pr ...
- 技术解析:锁屏绕过,三星Galaxy系列手机也能“被”呼出电话
近期,由两位安全研究人员,Roberto Paleari及Aristide Fattori,发布了关于三星Galaxy手机设备安全漏洞的技术细节.据称,Galaxy手机可在锁屏状态下被未授权的第三方人 ...
- 程序员是怎么炼成的---OC题集--练习答案与题目(1)
一. 1. 定义3个类,全部代码.效果:能默写,关键词全部正确. 2. ⾯面向对象和⾯面向过程有什么区别? 答:面向对象以事物(对象)为核⼼,完成事件只是一个任务.面向过程以事件为核心,为了完成任务, ...
- Configuring Squid as an accelerator/SSL offload for Outlook Web Access
reference:http://wiki.squid-cache.org/SquidFaq/ReverseProxy/ Configuring Squid as an accelerator/SSL ...