原题

题意:

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

思路:

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

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. 让Qt在MIPS Linux上运行 good

    下载 首先下载Qt everywhere,当前的版本是4.7.2,可以从nokia的网站上下载,也可以从git服务器上下载.考虑到文件有200M 以上的大小,下载速率低于25kBPS的,需要考虑从什么 ...

  2. c#实现类似数据的行锁

    当我们有一些这样的需求,比如某个订单中下单,修改等等这些是单例执行的,不能同步操作,当然这样的情况你可以使用数据库的行锁来实现,但是我们代码里面实现的话 ,我们也要用到锁,大部分情况下我们使用lock ...

  3. request的跳转

    使用request.getRequestDispather(url).forword(request,response)方法跳转页面 地址栏的路径不会发生改变,在后续的ajax调用 使用window. ...

  4. 解决npm install卡住不动的小尴尬

    npm install卡顿问题记录 遇到的问题 npm install -g @angular/cli 安装angular cli工具时,发现进度条一直卡住不动,相信很多朋友也遇到过.原因应该是国内的 ...

  5. 在C#中创建文件快捷方式

    创建快捷方式对于绝大多数 Windows 用户来说都是小菜一碟了,然而,这项工作却为程序员带来不少麻烦..NET 没有提供简便直接的创建快捷方式的方法,那么在 .NET 中我们如何为应用程序创建快捷方 ...

  6. Fabric1.4源码解析: 链码容器启动过程

    想写点东西记录一下最近看的一些Fabric源码,本文使用的是fabric1.4的版本,所以对于其他版本的fabric,内容可能会有所不同. 本文想针对Fabric中链码容器的启动过程进行源码的解析.这 ...

  7. MSVBVM60.dll中函数

    本文转载!!! 原文作者:飘云(飘云阁安全论坛) 原文地址:http://www.chinapyg.com/forum.php?mod=viewthread&tid=2225&high ...

  8. 前端Vue基础学习

    Vue基础 对与vue的简洁明了的干货分享,适合小白学习观看,如果有笔误或者想了解更多的,请给笔者留言,后续会继续分享,从零开始的vue相关经验 1.创建vue实例 <div id=" ...

  9. Java web环境的搭建

    学习java web 首先得要能够在自己的电脑上配置环境,经查阅资料后了解,需要装java EEEclipse ,即企业版的,还有就是tomcat服务器. 第一步,从网上下载,eclipse 企业版的 ...

  10. 8天入门docker系列 —— 第六天 搭建自己的私有镜像仓库Registry

    这一篇我们来聊聊私有仓库的搭建,其实不管你是通过docker build还是compose的方式进行构建,最终还是要将生成好的镜像push到远程的仓库中,这样多个 平台可以方便的获取你registry ...