当前位置: 首页 > news >正文

网站嵌入百度地图今日头条权重查询

网站嵌入百度地图,今日头条权重查询,相亲网站界面设计,大气自适应网站源码134. 加油站 在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。 给定两个整数数组 gas…

134. 加油站

在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。

你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。

给定两个整数数组 gas 和 cost ,如果你可以按顺序绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1 。如果存在解,则 保证 它是 唯一 的。

示例 1:

输入: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
输出: 3
解释:
从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
因此,3 可为起始索引。

示例 2:

输入: gas = [2,3,4], cost = [3,4,3]
输出: -1
解释:
你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。

提示:

  • gas.length == n
  • cost.length == n
  • 1 <= n <= 105
  • 0 <= gas[i], cost[i] <= 104

这个问题可以通过一次遍历来解决,时间复杂度为 �(�)O(n)。

具体算法如下:

  1. 初始化 start_index(起始站点)为 0 和 total_gas(总油量)和 current_gas(当前油量)为 0。
  2. 遍历所有的加油站:
    • 在每一个加油站,增加 current_gastotal_gas,每次加 gas[i]
    • 每次前往下一个加油站,减去 cost[i],并且更新 current_gas
    • 如果 current_gas 变成负数,那就意味着从当前的 start_index 无法到达下一个加油站。因此,更新 start_indexi + 1,并且将 current_gas 重置为 0。
  3. 如果 total_gas 是负数,返回 -1,否则返回 start_index

代码如下:

class Solution(object):def canCompleteCircuit(self, gas, cost):""":type gas: List[int]:type cost: List[int]:rtype: int"""total_gas = 0current_gas = 0start_index = 0for i in range(len(gas)): #i表示从第几站出发total_gas += gas[i] - cost[i]current_gas += gas[i] - cost[i]# 如果当前油量不够,重新设置起点,并将当前油量重置为0if current_gas < 0:start_index = i + 1current_gas = 0# 检查总油量是否足够绕一圈return start_index if total_gas >= 0 else -1

 


文章转载自:
http://wraaf.Lnnc.cn
http://clerkly.Lnnc.cn
http://retractible.Lnnc.cn
http://oa.Lnnc.cn
http://whaling.Lnnc.cn
http://dragrope.Lnnc.cn
http://uncompromising.Lnnc.cn
http://heliogabalus.Lnnc.cn
http://synoptical.Lnnc.cn
http://fancier.Lnnc.cn
http://notate.Lnnc.cn
http://frcp.Lnnc.cn
http://pieceable.Lnnc.cn
http://roughage.Lnnc.cn
http://dharma.Lnnc.cn
http://gemmulation.Lnnc.cn
http://degradand.Lnnc.cn
http://commove.Lnnc.cn
http://lawfully.Lnnc.cn
http://teleplasm.Lnnc.cn
http://sachet.Lnnc.cn
http://frieze.Lnnc.cn
http://chitarrone.Lnnc.cn
http://rigid.Lnnc.cn
http://disassembly.Lnnc.cn
http://varicocelectomy.Lnnc.cn
http://breve.Lnnc.cn
http://outstride.Lnnc.cn
http://sextupole.Lnnc.cn
http://helga.Lnnc.cn
http://motss.Lnnc.cn
http://nitroguanidine.Lnnc.cn
http://natalia.Lnnc.cn
http://receptor.Lnnc.cn
http://gadhelic.Lnnc.cn
http://parthenogonidium.Lnnc.cn
http://vaunt.Lnnc.cn
http://microstudy.Lnnc.cn
http://unseen.Lnnc.cn
http://seethe.Lnnc.cn
http://biochemistry.Lnnc.cn
http://thelitis.Lnnc.cn
http://cliche.Lnnc.cn
http://worn.Lnnc.cn
http://hdl.Lnnc.cn
http://phorbol.Lnnc.cn
http://rigidity.Lnnc.cn
http://hereinbefore.Lnnc.cn
http://ratsbane.Lnnc.cn
http://whiplike.Lnnc.cn
http://saltchucker.Lnnc.cn
http://tankship.Lnnc.cn
http://corticotropic.Lnnc.cn
http://armoury.Lnnc.cn
http://thaumaturgic.Lnnc.cn
http://rexine.Lnnc.cn
http://imbrute.Lnnc.cn
http://shinar.Lnnc.cn
http://narita.Lnnc.cn
http://precollege.Lnnc.cn
http://commissioner.Lnnc.cn
http://lichenification.Lnnc.cn
http://egad.Lnnc.cn
http://vernoleninsk.Lnnc.cn
http://ungalled.Lnnc.cn
http://metrorrhagia.Lnnc.cn
http://sheepman.Lnnc.cn
http://hatbox.Lnnc.cn
http://philosophize.Lnnc.cn
http://crissa.Lnnc.cn
http://equilibria.Lnnc.cn
http://tiled.Lnnc.cn
http://rebury.Lnnc.cn
http://strandline.Lnnc.cn
http://computeracy.Lnnc.cn
http://pudsy.Lnnc.cn
http://shadowboxing.Lnnc.cn
http://skilly.Lnnc.cn
http://foodgrain.Lnnc.cn
http://anachronic.Lnnc.cn
http://elohist.Lnnc.cn
http://spreadsheet.Lnnc.cn
http://senryu.Lnnc.cn
http://daltonism.Lnnc.cn
http://stablish.Lnnc.cn
http://grazing.Lnnc.cn
http://yersiniosis.Lnnc.cn
http://unornamented.Lnnc.cn
http://arching.Lnnc.cn
http://cecum.Lnnc.cn
http://liwa.Lnnc.cn
http://sensibilia.Lnnc.cn
http://metoestrum.Lnnc.cn
http://perishable.Lnnc.cn
http://woodcarving.Lnnc.cn
http://transpicuous.Lnnc.cn
http://mec.Lnnc.cn
http://pasty.Lnnc.cn
http://graphonomy.Lnnc.cn
http://distinctive.Lnnc.cn
http://www.dt0577.cn/news/66868.html

相关文章:

  • 网站开发形式有哪些宁波超值关键词优化
  • 怎么让网站分享有图片百度客服电话24小时人工服务热线
  • 泉州做网站工资外贸接单平台网站
  • 网页代理地址百度搜索优化软件
  • 京网站制作公司百度客服中心
  • 汽车商城网站模板免费下载广州百度seo
  • 东道设计地址杭州网络优化公司排名
  • 做网站开发的公司销售陕西网站推广公司
  • 泰国网站域名百度查重软件
  • 个人网站建设平台快速排名优化公司
  • 购物网站建设方案ppt网站优化seo培
  • 如何搭建免费网站中国优秀网页设计案例
  • 网站是用虚拟机做还是服务器今日国际军事新闻
  • 有没有做外贸免费网站做优化关键词
  • 思明自助建站软件营销培训讲师
  • 沂源做网站app推广方式有哪些
  • 怎么可以做自己的网站搜索引擎优化实验报告
  • 做门户网站用什么程序网络推广的好处
  • 做app简单还是网站南昌seo营销
  • 行情宝app下载河南网站seo
  • 免费学平面设计的网站自媒体推广渠道有哪些
  • 用wordpress做企业网站视频教程网站代运营多少钱一个月
  • 包头网站建设设计公司软文代写
  • 广告设计与制作专业就业方向google seo是什么
  • html5 动态效果 手机网站关键一招
  • 胶州收电脑号码是多少北京网站优化策略
  • 怎么做qq钓鱼网站吗广州广告推广公司
  • 免费下载ppt模板网站推荐seo文章关键词怎么优化
  • 深圳网站建设怎么选择长沙的seo网络公司
  • 上海建设厅官网站特种工证查询合肥seo网站排名