There are N gas stations along a circular route, where the amount of gas at station i isgas[i].

You have a car with an unlimited gas tank and it costscost[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.

C++

class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int total = 0, sum = 0;
int index = -1;
for (int i = 0; i < gas.size(); i++) {
sum += gas[i] - cost[i];
total += gas[i] - cost[i];
if (sum < 0) {
sum = 0;
index = i;
}
}
return total >= 0 ? index + 1 : -1;
}
};

最新文章

  1. linux 如何对文件解压或打包压缩
  2. 【转】 iOS9.2-iOS9.3.3越狱插件清单
  3. 《zw版&#183;Halcon入门教程与内置demo》
  4. Java文件读取
  5. 为什么玩VR眼镜会头晕?
  6. 熟练使用git命令
  7. lightoj1027
  8. HDFS Java API 常用操作
  9. 【PHP】iOS推送通知以及反馈服务
  10. CREATE PROCEDURE
  11. 基于visual Studio2013解决C语言竞赛题之0202坐标转换
  12. RPC框架
  13. Ubuntu下OpenVPN客户端配置教程
  14. 向量相加CUDA练习
  15. AJAX封装(IE)
  16. 浅析ConcurrentHashMap
  17. Activiti获取ProcessEngine的三种方法
  18. 关于Android中ION的libion
  19. 转载:删除github上文件夹的两种方式
  20. HTTP 请求头中的 X-Forwarded-For,X-Real-IP

热门文章

  1. matlab函数randperm()
  2. 328 day07线程池、Lambda表达式
  3. 求1+2+…+n
  4. CodeForce-792B Counting-out Rhyme(模拟)
  5. CodeForce-785B Anton and Classes(简单贪心)
  6. CORS跨域请求规则以及在Spring中的实现
  7. nginx负载轮询
  8. requests访问页面时set-cookie获取cookie
  9. NOI.AC#2144-子串【SAM,倍增】
  10. VueCLI3 创建vue项目