LeetCode _ 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.
思路: 类似KMP不回朔的思想。start表示开始的点,sum表示当前汽车的油量。当汽车到达汽油站i时如果不能到达下一站,则更新start直到可以使汽车能够从当前节点到达下一站,如果不存在,则把start设置为下一站。
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int len = gas.size();
if(cost.size() != len) return -;
vector<int> flag(len*, );
for(int i = ; i< len; i++)
flag[i] = gas[i] - cost[i];
for(int i = len ; i< len *; ++i)
flag[i] = flag[i-len];
int start = , sum = ;
for(int i = ; i< len + start && start < len; )
{
sum += flag[i] ;
if(sum >= ){
++i;
continue;
}
while(sum< && start < i){
sum -= flag[start];
++start;
}
i++;
if(sum < ){
sum = ;
start = i;
}
}
if(start <len)
return start;
return -;
}
};
LeetCode _ Gas Station的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 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】Gas Station
Gas Station There are N gas stations along a circular route, where the amount of gas at station i is ...
- 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 OJ] Gas Station
问题描述: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
- [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 ...
- 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 ...
随机推荐
- OperationalError:(1054 - "Unknown column 'game.lable1' in 'field list' ")解决办法
今天白天遇到一个错误,第一次遇到这样的问题,数据库的问题,百度了很多答案也找了很多博客文章看 问题:OperationalError:(1054 - "Unknown column 'gam ...
- jQuery.isEmptyObject()函数用于判断指定参数是否是一个空对象。
jquery中有一个函数isEmptyObject()用来判断制定参数是否是一个空对象. 示例如下: function isEmptyObject(e) { var t; for (t in e) r ...
- ipcs, ipcrm 命令
ipcs命令 是linux/uinx上提供关于一些进程间通信方式的信息,包括共享内存,消息队列,信号 ipcs用法 ipcs -a 是默认的输出信息 打印出当前系统中所有的进程间通信方式的信息 ip ...
- Android -- Messager与Service
如果你需要你的service和其他进程通信,那么你可以使用一个Messenger来提供这个接口. 这种方法允许你在不使用 AIDL的情况下,进行跨进程通信IPC. 实现步骤 下面是一个如何使用 Mes ...
- linux中切换用户方式su和su -的区别
Using su The su command allows users to open a terminal window, and from that terminal start a sub ...
- hdu 2544
#include <iostream> #include <cstdio> #define INF 9999999 //#define INF 0x3f3f3f3 using ...
- Examples_06_02(android)DDMS的data文件中没有显示文件。
以前这里不显示music.cfg.通过Reset adb,就显示了. 查看虚拟机运行时里面的文件,进入adb.exe目录: E:\TDDOWNLOAD\adt-bundle-windows-x86-2 ...
- 3.RxJava详解
一.RxJava 到底是什么 异步(取代AsyncTask/Handler/XXX/...?) 二.RxJava 好在哪 简洁(逻辑的简洁,.一步一走) 举例: 题目:将文件夹中的图片都取 ...
- iptables 下开放ftp
这两天在给客户安装服务器时也顺便给他们使用iptables,不用不知道,一用才发现iptables还有很多东西可以学的,比如开放ftp.iptables 的filter表的INPUT链的默认策略设为了 ...
- MySQL利用binlog来恢复数据库
1.根据binlog解析出所有ring数据库的所有sql [mysql@localhost ]$ mysqlbinlog --no-defaults --database=ring --start-d ...