题目

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.

分析

题目描述:有N个加油站点构成一个环状,每个站点i加油量为gas[i],从站点i到站点i+1需要耗费有两为cost[i],现要求从哪个站点出发可以成功转一圈回到初始站点,返回该站点,若没有则返回-1;

详细思路分析参考:算法分析网址

AC代码

class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
if (gas.size() == 0 || cost.size() == 0 || gas.size() != cost.size()) return -1;
int total = 0, sum = 0, start = 0;
for (int i = 0; i < gas.size(); ++i){
total += (gas[i] - cost[i]);
if (sum < 0){ //发现油箱空了,从下一个站点尝试
sum = (gas[i] - cost[i]);
start = i;
}
else
sum += (gas[i] - cost[i]);
}
return total < 0 ? -1 : start; //用total判断start 是否是满足要求的解
}
};

LeetCode(134) Gas Station的更多相关文章

  1. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  2. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  3. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  4. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  5. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  6. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  7. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. IIS中的 Asp.Net Core 和 dotnet watch

    在基于传统的.NET Framework的Asp.Net Mvc的时候,本地开发环境中可以在IIS中建立一个站点,可以直接把站点的目录指向asp.net mvc的项目的根目录.然后build一下就可以 ...

  2. ES6:string.raw浅析

    当前正学习ES6 ,遇到string.raw费心思,现将试验后的结果整理如下: 网上得来的试验: 语法 String.raw`templateStr`; String.raw(obj, ...subs ...

  3. (转)AIX 中 Paging Space 使用率过高的分析与解决

    AIX 中 Paging Space 使用率过高的分析与解决 原文:https://www.ibm.com/developerworks/cn/aix/library/au-cn-pagingspac ...

  4. Zeppelin的入门使用系列之使用Zeppelin来运行Spark SQL(四)

    不多说,直接上干货! 前期博客 Zeppelin的入门使用系列之使用Zeppelin来创建临时表UserTable(三) 1. 运行年龄统计的Spark SQL (1)  输入Spark SQL时,必 ...

  5. 【转】"超时时间已到。在操作完成之前超时时间已过或服务器未响应"的解决方法

    方法有以下三种: 1.原因应该在数据访问有问题,可以把连接时间设置长些,在数据库连接字符串,加上Connect Timeout=18000,单位毫秒 2,在web.config中加上以下语句: < ...

  6. Tomcat一

    Tomcat是如何处理http请求的 Tomcat有什么用? Tomcat是一个应用服务器,也是一个Servlet容器,用来接收前端传过来的请求,并将请求传给Servlet,并将Servlet的响应返 ...

  7. SQL基本语法备忘

    注:以下演示是在mysql命令行下的操作 数据库相关操作 create database mytest; --创建数据库 create database if not exists mytest; - ...

  8. 洛谷 P2424 约数和

    题目背景 Smart最近沉迷于对约数的研究中. 题目描述 对于一个数X,函数f(X)表示X所有约数的和.例如:f(6)=1+2+3+6=12.对于一个X,Smart可以很快的算出f(X).现在的问题是 ...

  9. (转)SQL注入攻击简介

    如果你是做Javaweb应用开发的,那么必须熟悉那声名狼藉的SQL注入式攻击.去年Sony就遭受了SQL注入攻击,被盗用了一些Sony play station(PS机)用户的数据.在SQL注入攻击里 ...

  10. 2015 ACM/ICPC Asia Regional Changchun Online Pro 1005 Travel (Krsukal变形)

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...