Gas Station
来源:互联网 发布:云计算公司大约市值 编辑:程序博客网 时间:2024/06/08 12:07
题目描述:
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.
分析:
考虑从每个点出发的可行性即可
代码:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { if(gas.size()==1) { if(gas[0]>=cost[0]) return 0; return -1; } for(int i=0;i<gas.size();i++){ int num=0; int ok=0; int j; for( j=0;j<gas.size();j++){ int s=i+j; if(s>=gas.size()) s=s%gas.size(); num+=gas[s]; num-=cost[s]; if(num>=0){ ok++; if(ok==gas.size()) return i; } else{ break; } } } return -1; }
阅读全文
0 0
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Gas Station
- Java多线程编程-(11)-面试常客ThreadLocal出现OOM内存溢出的场景和原理分析
- C#中的委托和事件
- layer.open打开iframe页面的调用父页面方法及关闭
- 最大似然估计和最小二乘法
- 最牛逼的Linux性能分析工具网站
- Gas Station
- 《算法图解》第一章学习
- Gartner公布2018十大战略科技发展趋势,未来5年AI将无处不在
- SQL查询库、表存储容量大小
- 空结构体多大?
- 群晖Synology DSM6.1 使用国内docker镜像加速
- Linux 安装 Oracle11g R2 详细教程
- 行为识别阅读笔记(paper+code):Real-time Action Recognition with Enhanced Motion Vector CNNs
- layer父子页面交互