原题

题意:

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

思路:

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

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. Zookeeper zkui-zookeeper图形化管理工具

    zkui zkui是一个Zookeeper可视化管理工具. Github:https://github.com/DeemOpen/zkui zkui安装 1.Git拉取代码 #git clone ht ...

  2. Windows Python虚拟环境配置(Distribute + pip + virtualenv + virtualenvwrapper-powershell)

    对于Python开发新手,很多人会迷茫那些各种名目的工具和概念,如Python2.7, Python3.3, Distribute, pip, virtualenv,Setuptools, easy_ ...

  3. shell把文件批量导入mysql

    for file in ./tmp_data/* do echo $file mysql -u'root' -p'wangbin' --default-character-set=utf8 -e&qu ...

  4. Laravel中我们登录服务器通过 Tinker 手动创建后台管理用户

    Laravel中我们可以登录到服务器通过 Tinker 手动创建后台用户 laravel中tinker命令的作用是用来调试laravel,可以打印变量或对象信息,显示函数代码,对数据库写入和查询数据. ...

  5. [转]深入了解iPad上的MouseEvent

    iPad上没有鼠标,所以手指在触发触摸事件(TouchEvent)的时候,系统也会产生出模拟的鼠标事件(MouseEvent).      这对于普通网页的浏览需求而言,基本可以做到与PC端浏览器无明 ...

  6. 写在Logg SAP项目上线之际

    根据大环境大行业的惯用做法,公司建立Logg品牌是在意料之中.毫无意外的,Logg也要上到SAP系统中. 其实按它的业务模式来说上SAP系统并不困难,早在几年前就已经有做过了.无非就是接单不生产,外包 ...

  7. Free MP3 CD Ripper_缓冲区溢出远程代码执行_CVE-2019-9766漏洞复现

    Free MP3 CD Ripper_缓冲区溢出远程代码执行_CVE-2019-9766漏洞复现 一.漏洞描述 Free MP3 CD Ripper是一款音频格式转换器.Free MP3 CD Rip ...

  8. 从Excel到Python 数据分析进阶指南

    目 录   第1章 生成数据表 第2章 数据表检查 第3章 数据表清洗 第4章 数据预处理 第5章 数据提取 第6章 数据筛选 第7章 数据汇总 第8章 数据统计 第9章 数据输出 案例 990万次骑 ...

  9. 【工具】读取proprtties工具类

    获取properties内容: 基本的使用看网络上大多是这样的,使用时注意线程安全以及读写的实时性问题. 1.直接通过流读取(反射): InputStream inStream =  this.get ...

  10. JavaScript 常见的六种继承方式

    JavaScript 常见的六种继承方式 前言 面向对象编程很重要的一个方面,就是对象的继承.A 对象通过继承 B 对象,就能直接拥有 B 对象的所有属性和方法.这对于代码的复用是非常有用的. 大部分 ...