【LeetCode】加油站
【问题】在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。
说明:
如果题目有解,该答案即为唯一答案。
输入数组均为非空数组,且长度相同。
输入数组中的元素均为非负数。
输入:
gas = [,,,,]
cost = [,,,,]
输出:
解释:
从 号加油站(索引为 处)出发,可获得 升汽油。此时油箱有 = + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,此时油箱有 - + = 升汽油
开往 号加油站,你需要消耗 升汽油,正好足够你返回到 号加油站。
因此, 可为起始索引。
【思路】首先我们要分析这个起点会不会存在?这个其实很简单,如果将所有的gas总油量 >= 车子的cost总耗油量,从而车子可以走一周的。
我们分析一下,假设起始点为x, 如果总的gas-总的cost大于零时,则我们将当前路径从其实路径x一分为二,左侧的部分剩余油量必定 <= 0,而右侧的部分剩余油量必定 >= 0. 这样才可以使得车子可以完成一周的路程!
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int total = , cur = , start = ;
for(int i = ; i < gas.size(); i++){
total += (gas[i] - cost[i]);
cur += (gas[i] - cost[i]);
if(cur < ){
start = i + ;
cur = ;
}
}
return (total >= ) ? start : -;
}
};
【LeetCode】加油站的更多相关文章
- [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- LeetCode:加油站【134】
LeetCode:加油站[134] 题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要 ...
- [LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数
A car travels from a starting position to a destination which is target miles east of the starting p ...
- leetcode 134 加油站问题
leetcode 134 解析 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 co ...
- [LeetCode] Gas Station 加油站问题
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 【leet-code】135. 加油站
题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...
- [Leetcode 134]汽车加油站 Gas Station (环形)
[题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i] ...
- [Leetcode]134.加油站
这一题是贪心不是模拟 是贪心不是模拟 是贪心不是模拟! 如果用模拟的做法会比较慢,也失去了做这一题的趣味了. 模拟的方法很简单,就是每一个加油站都做起点模拟一遍,试一下能不能完成一圈,能完成一圈就保存 ...
- [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 ...
- LeetCode OJ:Gas Station(加油站问题)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- 操作系统OS,Python - 协程(Coroutine)
留坑 参考: https://en.wikipedia.org/wiki/Coroutine https://zh.wikipedia.org/wiki/%E5%8D%8F%E7%A8%8B http ...
- Openstack----学习笔记
ceph 分布式存储,用于存放新创建的云主机磁盘镜像文件和磁盘 创建云主机流程记录 简易版本: 上图中所有发送的请求都会存放在rabbit_mq(消息队列)中,各个组件会定时取消息队列中与自己相关的请 ...
- Atcoder Grand Contest 037B(DP,组合数学,思维)
#include<bits/stdc++.h>using namespace std;const long long mod = 998244353;string s;int a[3000 ...
- android开发如何在页面之间传参
第一个页面跳转 传递值 Button bn1=(Button)findViewById(R.id.btn_Login); //跳转bn1.setOnClickListener(new OnClickL ...
- nacos集群配置
一. 环境准备 Nacos 依赖 java环境来运行.如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在以下版本环境中安装使用: 64 bit OS,支持 Lin ...
- JSON转换的实现
String转成JSON这个依赖很重要,我们将围绕fastjson中的JSONObject这个类来谈转换 <dependency> <groupId>com.alibaba&l ...
- 2.分析Ajax请求并抓取今日头条街拍美图
import requests from urllib.parse import urlencode # 引入异常类 from requests.exceptions import RequestEx ...
- Python 基础之import导包
首先需要将import内容建立一个大概如下层级的包: 以黑色框为第一级,蓝色框为第二级,棕色框为第三级,红色框为第四级 一.import 引入初识 首先在module.py写入代码如下: xboy = ...
- 本地虚拟机搭建ES集群
一.环境说明 1.物理机信息(主要): 内存:8G 系统/主频:Win7(旗舰版)64位/3.70GHZ 2.虚拟机信息: VMware Workstation 14 Pro 下载地址: 链接:htt ...
- LeetCode 1025. Divisor Game
题目链接:https://leetcode.com/problems/divisor-game/ 题意:Alice和Bob玩一个游戏,Alice先开始.最初,黑板上有一个数字N.每一轮,选手首先需要选 ...