原题

题意:

过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈。

思路:

一开始思路就是遍历一圈,最直接的思路。

class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int beg = 0;
int tank = 0;
int n = gas.size();
int sumGas = 0, sumCost = 0;
for (auto a : gas)
sumGas += a;
for (auto a : cost)
sumCost += a; if (sumGas < sumCost)
{
return -1;
} for (int i = 0; i < n; i++)
{
tank += gas[i]; if (tank >= cost[i])
{
tank -= cost[i];
}
else
{
beg = i + 1;
tank = 0;
}
}
return beg;
}
};

看到别人的题解,很简洁。

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

[leetcode] 134. Gas Station (medium)的更多相关文章

  1. leetcode 之Gas Station(11)

    这题的思路很巧妙,用两个变量,一个变量衡量当前指针是否有效,一个衡量整个数组是否有解,需要好好体会. int gasStation(vector<int> &gas, vector ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  6. [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 ...

  7. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  8. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  9. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

随机推荐

  1. Using 3D engines with Qt(可以整合到Qt里,不影响)

    A number of popular 3D engines can be integrated with Qt: Contents [hide]  1 Ogre 2 Irrlicht 3 OpenS ...

  2. Qt编程规范

    一.概述 良好的编程规范可以大幅提高一个程序的可读性.可理解性和可维护性. 本规范参考Effective C++中文版.Google C++编码规范及Qt编码风格. 二.头文件 1)      #de ...

  3. 利用系统自带工具快速实现SSH免密远程登录

    一.软件环境 操作系统:CentOS Linux release 7.4.1708 (Core) SSH版本 [root@Geeklp201 ~]# ssh -V OpenSSH_7.4p1, Ope ...

  4. J2EE--Struts2基础开发

    内容中包含 base64string 图片造成字符过多,拒绝显示

  5. WSAAsyncSelect Demo

    #include <WinSock2.h> #include <Windows.h> #include <StrSafe.h> #pragma comment(li ...

  6. 解决socket.error: [Errno 98] Address already in use问题

    如果python中socket 绑定的地址正在使用,往往会出现错误, 在linux下: 则会显示“ socket.error: [Errno 98] Address already in use” 在 ...

  7. 生产环境MySQL优化

    a:硬件的优化: 1. 采用64位cpu,cpu至少4颗,L2缓存越大越好2. 内存要大,32-64G运行1-2个实例,96-128G运行3-4个实例3. 机械盘选用sas盘,转速15000以上,有可 ...

  8. CS代码代写, 程序代写, java代写, python代写, c/c++代写,csdaixie,daixie,作业代写,代写

    互联网一线工程师程序代写 微信联系 当天完成特色: 互联网一线工程师 24-48小时完成.用心代写/辅导/帮助客户CS作业. 客户反馈与评价 服务质量:保证honor code,代码原创.参考课程sl ...

  9. tkinter + cefpython 仿美团桌面程序

    使用js开发桌面程序目前是一个趋势,Electron是其中一个佼佼者,网上也不乏很多文章.今天主要是来讲一下cefpython. 用python的朋友,特别使用过tkinter开发过界面的,一定会觉得 ...

  10. vue.js执行mounted的实例

    代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...