当前位置:网站首页>134. 加油站
134. 加油站
2022-06-11 07:43:00 【zzu菜】
在一条环路上有 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 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。
思路
首先创建一个数组 nums 保存这个加油站所能加的油和消耗的油之差。
同时记录sum 看看总油数和总里程数之差。
即
for (int i =0;i<nums.length;i++){
nums[i] = gas[i] - cost[i];
sum = nums[i]+sum;
}
所以如果sum<0,肯定是要返回-1的
sum>0一定有解,需要具体分析出具体坐标
sum>0:
利用nums数组
gas = [1,2,3,4,5], cost = [3,4,5,1,2],nums=[-2,-2,-2,3,3]
我们需要找到一个startIndex坐标,可以从该点开始行驶在回到startIndex坐标
我们利用temp记录每轮剩下的油耗
如果行驶到下一个汽油站时temp为负时肯定不行,
换下一个startIndex,直到找到一个startIndex
if(sum>=0){
int temp=0;
int startIndex = 0;
while (true){
for (int i=startIndex;i<nums.length+startIndex;i++){
if(startIndex==nums.length-1) return startIndex;
int indexTemp = 0;
if(i<nums.length) indexTemp=i;
else indexTemp = i-nums.length;
temp = temp + nums[indexTemp];
if(temp<0){
temp=0;
startIndex = i+1;
break;
}
if(i==startIndex+nums.length-1&&temp>=0){
return startIndex;
}
}
}
}
完整代码
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int[] nums=new int[gas.length];
int max = Integer.MIN_VALUE;
int index = -1;
int sum = 0;
for (int i =0;i<nums.length;i++){
nums[i] = gas[i] - cost[i];
sum = nums[i]+sum;
}
if(sum>=0){
int temp=0;
int startIndex = 0;
while (true){
for (int i=startIndex;i<nums.length+startIndex;i++){
if(startIndex==nums.length-1) return startIndex;
int indexTemp = 0;
if(i<nums.length) indexTemp=i;
else indexTemp = i-nums.length;
temp = temp + nums[indexTemp];
if(temp<0){
temp=0;
startIndex = i+1;
break;
}
if(i==startIndex+nums.length-1&&temp>=0){
return startIndex;
}
}
}
}
else return -1;
}
}
边栏推荐
- 2022 low voltage electrician test questions and online simulation test
- Simple configuration of vscade
- Zero foundation self-study SQL course | outer join external connection
- Seata的几种事务模式
- 运筹学导论
- Simple use of string
- Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
- Implementation of stack (C language)
- multi-sig SC
- The maximum number of divisors of numbers in the int range is 1536
猜你喜欢

Simple configuration of vscade

2022 low voltage electrician operation certificate test question simulation test platform operation

二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...

C language lesson 2

C language inherits memory management mechanism (unfinished)

Flask页面的分页

【IoT】项目管理:如何打造更好的跨职能团队?

2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings

【Oracle 数据库】奶妈式教程day03 排序查询

如何准备PMP新版大纲考试?
随机推荐
The maximum number of divisors of numbers in the int range is 1536
Paging of the flask page
Qstring to hexadecimal qstring
Configuration software -- control drag and drop
二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]
What is the difference between gaussdb for redis and redis?
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products
MFC custom string linked list
Sdl-3 YUV playback
C language Yanghui triangle code
QT custom control library creation
String Simulation Implementation
Qunhui ds918 creates m.2 SSD read / write cache
Rabin-Miller素数测试
[atcoder2305] declining (game)
【AtCoder2387】+/- Rectangle
Djikstra solves the shortest circuit with negative weight
2021-11-05 definition of cache
What is the lifecycle of automated testing?