当前位置:网站首页>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;
}
}
边栏推荐
- 避免list的并发修改异常的几种方式
- 签到体系设计:签到功能该怎么画
- A detailed explanation of one of the causes of dead loop caused by array out of bounds in C language
- 代码设置ConstraintLayout的layout_constraintDimensionRatio
- Clipping and overlapping of YUV data
- forEach 中 return 和 for 中 break
- Bladed入門教程(視頻)
- 2022.6.7 特长生模拟
- C language - growth diary-04- preliminary exploration of local variables (local variables)
- 排序——归并排序
猜你喜欢

二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...

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

Servlet

群晖DS918创建m.2 固态硬盘SSD读写缓存

Using Tkinter to realize guessing numbers game
![[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

C language to achieve a simple game - minesweeping

Implementation of stack (C language)

Xshell7 和 Xftp7要继续使用此程序,您必须应用最新的更新或者使用新版本

Understanding of Poisson distribution and Poisson process and Erlang distribution and their relations (important theories in queuing theory and operational research)
随机推荐
【IoT】项目管理:如何打造更好的跨职能团队?
学习《缠解论语》
Request request object and response response object
记一次忽略@SuppressLint(“NewApi“)提示引发的血案
Summary of evaluation index knowledge points in target detection: summary of IOU cross overlap unit and map/ap/tp/fp/np
二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...
Database connection pool and bdutils tool
20200803 T3 my friends [divide and conquer NTT optimization recursion]
After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry
Long dialogue in June 2017
[atcoder1980] mystious light (mathematical simulation)
批量拼接字符串
How to prepare for the new PMP syllabus exam?
C. Manipulating History(贪心/哈希/思维/好题)
【案例解读】医疗单据OCR识别助力健康险智能理赔
You got 8K in the 3-year function test, but you were actually pretending to work hard
【AtCoder2304】Cleaning
Detailed explanation of character function and string function (including simulation implementation)
JSP technology: JSP overview, JSP basic syntax, JSP instructions, JSP implicit objects, JSP action elements
Classes and objects (medium)