[leetcode] 134. Gas Station (medium)
原题
题意:
过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈。
思路:
一开始思路就是遍历一圈,最直接的思路。
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)的更多相关文章
- leetcode 之Gas Station(11)
这题的思路很巧妙,用两个变量,一个变量衡量当前指针是否有效,一个衡量整个数组是否有解,需要好好体会. int gasStation(vector<int> &gas, vector ...
- 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 ...
- [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 ...
- 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 ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- [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 ...
- LeetCode: 61. Rotate List(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
随机推荐
- Linux下卸载QT SDK
unbuntu下卸载QT方法一:you can remove it like this, those developers should add this somewhere ! like next ...
- 在Qt工程中加Boost
摘要: Boost是一个很强大的C++库,堪比STL,里面有很多非常优秀的类库.我不多介绍,详情见官网:http://www.boost.org/ 要在我们的Qt工程中把这个库加进去应该怎么做呢?我今 ...
- 使用熔断器仪表盘监控(hystrix)
概述 在 Ribbon 和 Feign 项目增加 Hystrix 仪表盘功能,两个项目的改造方式相同. 在 pom.xml 中增加依赖 <dependency> <groupId&g ...
- l论文查重平台
推荐大家一个靠谱的论文检测平台.重复的部分有详细出处以及具体修改意见,能直接在文章上做修改,全部改完一键下载就搞定了.怕麻烦的话,还能用它自带的降重功能.哦对了,他们现在正在做毕业季活动, 赠送很多免 ...
- BootStrap4.0Demo+轮播素材记录
整理一些关于前端的东西: BootStrap4.0Demo: 官方DEMO:http://code.z01.com/v4/components/carousel.html 下午翻了点不错的轮播素材: ...
- python argparse模块的使用
import argparse def get_parse(): # 初始化 parse = argparse.ArgumentParser() # 添加选项,类型为str,默认为空 parse.ad ...
- CentOS 7使用Elasticsearch
安装ElasticSearch 下载依赖 Elasticsearch依赖jdk, 在官网下载jdk压缩包, 或者直接安装. 下载压缩包, 解压tar -xzvf jdk-8u181-linux-x64 ...
- 关于火狐浏览器设置cookie的一个问题
最近发现我一个项目的网页,里面的cookie无法添加了,急的我瞪着我的PHP代码沉思了好久,我默认用的火狐浏览器,然而我默默的打开另一个叫360的浏览器,发现它的cookie是正常添加的. ... 难 ...
- 理论+实践解析“IT治理”之模式与原则
IT治理工作作为企业信息化建设的上层建筑,扮演着及其重要的角色.本文结合作者的学习及实践经验给出一些借鉴. 一.IT治理概述 1.1 何为IT治理 在企业信息化建设中的最大问题,往往不是技术问题,也不 ...
- white box白盒测试
逻辑覆盖法:语句覆盖,判定覆盖,条件覆盖,判定/条件覆盖,组合覆盖,路径覆盖 基本路径测试法:Control Flow Graphs, CFG.带箭头的边 条件覆盖:使每个判定中每个条件的可能值至少满 ...