134. Gas Station (Array; DP)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
思路:
1. 是否能travel around? =>只要gas[]加总的和 - cost[]加总的和 > 0,就能够。=>需要一个INT计算gas[]-cost[]的总合
2. 如何找到起点?如果到了某一站油不够,那么说明之前节点作为起点不行,起点必须从它后面开始重新找。=>需要一个INT记录从找到的起点到目前为止的油量
时间复杂度O(n)
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int sum = , total = , len = gas.size(), index = -;
for(int i=; i<len; i++){
sum += gas[i]-cost[i];
total += gas[i]-cost[i];
if(sum < ){
index = i;
sum = ;
}
}
return total>= ? index+ : -;
}
};
134. Gas Station (Array; DP)的更多相关文章
- 134. Gas Station leetcode
134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...
- 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 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 (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 ...
- 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. 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
Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ...
- 134. Gas Station加油站
[抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ...
随机推荐
- airtest IDE问题汇总
FAQ 1.同一个脚本,使用IDE可以运行,使用命令行运行报错 原因:曾经开启过anyproxy代理,添加过HTTP_PROXY环境变量,将其取消即可 unset HTTP_PROXY https:/ ...
- [UE4]单映射:TMap容器,字典表
一.TMap是什么 TMap是UE4中的一种关联容器,每个键都关联着一个值,形成了单映射关系.因此你可以通过键名来快速查找到值.此外,单映射要求每个键都是唯一的. 二.创建和填充单映射 如果你想创建一 ...
- Android RIL概述
前言 Android作为一个通用的移动平台,其首要的功能就是通话.短信以及上网等通信功能.那么,从系统的角度来看,Android究竟是怎么实现与网络的交互的了? 这篇文章里,就来看一看Android中 ...
- python入门-WHILE循环
1 使用while循环 current_number= : print(current_number) current_number += 2 让用户选择退出 prompt = "tell ...
- phpmyadmin登录提示2005错误
请修改phpMyAdmin\libraries\config.default.php 把 $cfg['Servers'][$i]['host'] 改成127.0.0.1
- linux7系统开机报错failed to start login service
1.开机报错failed to start login service 参考网站:https://unix.stackexchange.com/questions/264994/kali-sudden ...
- LINUX漏洞-安全防护--防火墙相关
漏洞扫描 https://blog.csdn.net/e_Inch_Photo/article/details/79072360 基本安全防范: https://blog.csdn.net/holmo ...
- 65. sqlserver执行存储过程实例
declare @param varchar(500)exec sp_PUB_GetFlowStatus @ret output,10011,88,1,12print @ret
- controller检查header
以前都只能拿到request再检查,其实有相应的注解. public Result updateRecentScore(@RequestBody Map map, @RequestHeader(&qu ...
- HTML5 浏览器接收的常用 content-type
<1> 常见的设置方法 response.setHeader("content-type", 'text/html'); <2> 浏览器接收的常用 cont ...