Gas Station [LeetCode]
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.
Solution:
The brute force solution costs O(n^2), I find a O(n) and one round solution. If gas[i] >= cost[i], then I mark it, and calcuate the gas in tank along next node, if it is less than zero, then restart search.
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int curr_gas = ;
int idx = -;
int tmp_gas = ;
for(int i = ; i < gas.size(); i ++) {
curr_gas += gas[i] - cost[i];
if( idx != -)
tmp_gas += gas[i] - cost[i]; if(tmp_gas < ) {
idx = -;
tmp_gas = ;
} if(gas[i] - cost[i] >= && idx == -) {
idx = i;
tmp_gas += gas[i] - cost[i];
}
} if(curr_gas >= )
return idx;
else
return -;
}
Gas Station [LeetCode]的更多相关文章
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- Gas Station——LeetCode
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Gas Station leetcode java
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- Gas Station|leetcode 贪心
贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...
- Gas Station [leetcode] 两个解决方案
因为gas的总数大于cost总时间.你将能够圈住整个城市. 第一溶液: 如果一開始有足够的油.从位置i出发.到位置k时剩余的油量为L(i,k). 对随意的k.L(i,k)依据i的不同,仅仅相差常数. ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- [LeetCode] Gas Station
Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...
- 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 ...
随机推荐
- Cheatsheet: 2014 05.01 ~ 05.31
Web Choosing a Web Framework/Language Combo for the Next Decade Optimizing NGINX and PHP-fpm for hig ...
- left join 等连接查询遇到同名字段覆盖问题
可以在查询时给字段赋别名,但是需要注意以下:*的位置要在最前面,放在其他地方都会出错.这种写法同名覆盖的字段还在,然后在*的后面加上别名字段,已经可以满足所有需求了 SELECT *,r.id as ...
- Python学习遇到的问题
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position
- SQL GUID和自增列做主键的优缺点
我们公司的数据库全部是使用GUID做主键的,很多人习惯使用int做主键.所以呢,这里总结一下,将两种数据类型做主键进行一个比较. 使用INT做主键的优点: 1.需要很小的数据存储空间,仅仅需要4 by ...
- 线程入门之优先级priority
package com.thread; /** * 优先级: * Thread.MAX_PRIORITY:最大优先级 10 * Thread.MIN_PRIORITY:最小优先级 1 * Thread ...
- hdu 5023 A Corrupt Mayor's Performance Art 线段树
A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100 ...
- bzoj 1208: [HNOI2004]宠物收养所 set
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7328 Solved: 2892[Submit][Sta ...
- php 上传文件。$_FILES
<form name="article" method="post" enctype="multipart/form-data" ac ...
- 百度web前端面试2015.10.18
邮件里通知的周日下午两点参加百度校招面试,我13:10分就到了,前台先让我拿了个面试资格单(上面是我的信息),然后在web前端面试入口排队,面试在百度食堂举行的,等了大概1个小时,放我去面试.都是一对 ...
- 2014 Multi-University Training Contest 1
A hdu4861 打表找规律 #include <iostream> #include<cstdio> #include<cstring> #include< ...