当前位置:网站首页>134. gas station
134. gas station
2022-06-11 07:50:00 【Zzu dish】
There is... On a ring road n A gas station , Among them the first i Gas stations have gas gas[i] l .
You have a car with unlimited tank capacity , From i A gas station goes to the i+1 A gas station needs gas cost[i] l . You start from one of the gas stations , The tank is empty at first .
Given two arrays of integers gas and cost , If you can drive around the loop , Then return to the number of the gas station at the time of departure , Otherwise return to -1 . If there is a solution , be Guarantee It is only Of .
Example 1:
Input : gas = [1,2,3,4,5], cost = [3,4,5,1,2]
Output : 3
explain :
from 3 Gas station ( The index for 3 It's about ) set out , Can be obtained 4 A litre of petrol . Now the tank has = 0 + 4 = 4 A litre of petrol
Go to 4 Gas station , Now the tank has 4 - 1 + 5 = 8 A litre of petrol
Go to 0 Gas station , Now the tank has 8 - 2 + 1 = 7 A litre of petrol
Go to 1 Gas station , Now the tank has 7 - 3 + 2 = 6 A litre of petrol
Go to 2 Gas station , Now the tank has 6 - 4 + 3 = 5 A litre of petrol
Go to 3 Gas station , You need to consume 5 A litre of petrol , Just enough for you to return to 3 Gas station .
therefore ,3 Can be the starting index .
Example 2:
Input : gas = [2,3,4], cost = [3,4,3]
Output : -1
explain :
You can't 0 Number or 1 Gas station No.1 starts , Because there's not enough gas to get you to the next gas station .
We from 2 Gas station No.1 starts , You can get 4 A litre of petrol . Now the tank has = 0 + 4 = 4 A litre of petrol
Go to 0 Gas station , Now the tank has 4 - 3 + 2 = 3 A litre of petrol
Go to 1 Gas station , Now the tank has 3 - 3 + 3 = 3 A litre of petrol
You can't go back 2 Gas station , Because the return journey costs 4 A litre of petrol , But your tank only has 3 A litre of petrol .
therefore , No matter what , You can't even drive around the loop .
Ideas
First create an array nums Save the difference between the amount of fuel this gas station can fill and the amount of fuel it consumes .
Simultaneous recording sum Look at the difference between the total fuel and the total mileage .
namely
for (int i =0;i<nums.length;i++){
nums[i] = gas[i] - cost[i];
sum = nums[i]+sum;
}
So if sum<0, It must be to return -1 Of
sum>0 There must be a solution , Specific coordinates need to be analyzed
sum>0:
utilize nums Array
gas = [1,2,3,4,5], cost = [3,4,5,1,2],nums=[-2,-2,-2,3,3]
We need to find one startIndex coordinate , You can start at this point and drive back to startIndex coordinate
We make use of temp Record the remaining fuel consumption per round
If you drive to the next gas station temp Not when it is negative ,
For a startIndex, Until you find one 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;
}
}
}
}
Complete code
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;
}
}
边栏推荐
- Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
- Rabin-Miller素数测试
- [cluster] haproxy load balancing
- 【AtCoder2387】+/- Rectangle
- 20200802 T3 I always like [generating function exclusion, Lagrangian inversion]
- 2021-10-24
- A correction book full of sad tears
- Classes and objects (medium)
- 2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings
- 【AtCoder2304】Cleaning
猜你喜欢

【案例解读】医疗单据OCR识别助力健康险智能理赔

Collation of basic knowledge of intermediate development of Andrews (for interview)

C wechat upload form data
![[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products](/img/85/5766d8269391820b5e142178530657.png)
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products

Import on CSDN MD file

签到体系设计:签到功能该怎么画

记一次忽略@SuppressLint(“NewApi“)提示引发的血案
![[software testing] 90% of the interviewers have been brushed out of such resumes](/img/2f/bb4819b98592f750dec92d4b4dd6b7.png)
[software testing] 90% of the interviewers have been brushed out of such resumes

Tidb cloud launched Google cloud marketplace, empowering global developers with a new stack of real-time HTAP databases

Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...
随机推荐
【AtCoder2387】+/- Rectangle
图数据库无缝集成Tushare接口
Euler's theorem and its extension (with proof)
[atcoder2000] leftmost ball (dp+ combination number)
String Simulation Implementation
C- print 99 multiplication table
排序——归并排序
The solution of "no startup device" after running Bochs
C language - Growth Diary -03- function definition and function prototype declaration
[poj3691] DNA repair (AC automata +dp)
Use of wordcloud
C language - Growth Diary -02- function
【AtCoder2304】Cleaning
【AtCoder1981】Shorten Diameter(图论思维)
C language - Growth Diary -01- count primes and sum
[atcoder2305] declining (game)
Clipping and overlapping of YUV data
Introduction to operations research
安卓初中级开发基础知识整理(面试自用)
ConstraintLayout中使用Guideline限制控件最大宽度