当前位置:网站首页>leetcode 134. gas station
leetcode 134. gas station
2022-04-23 00:39:00 【Sauce bear】
Topic link
Ideas : One traverse
analysis : Then start from each site and try
however , Start site , The fuel that can be added must be greater than or equal to the fuel consumption to the next station .
namely gas[i]>=cost[i]
And because the answer is only , So the amount of oil you can add is definitely not 0
namely gas[i]!=0
Assuming that the x Leaves at , It's the farthest y, Suppose you don't go through all the sites .
So the next starting point , No x Post site , It should be y The following sites .
because x and y The middle site , It's past , I've judged that I can't .
Code :
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int len = gas.length;
int max =0;
int start =0;
for(int i=0;i<len;i++){
// The starting point must be The oil you can add is more than the gasoline you spend And because the answer is only , So the starting point is definitely not refueling 0
if(gas[i]>=cost[i] && gas[i]!=0){
//start Record the starting point
start = i;
// Record the amount of oil at this time , It must have been 0
int t =0;
// Record the sites you passed
int count=0;
// Judge whether there is enough oil at the next station also Judge whether you have run through all sites
while(t+gas[start%len]-cost[start%len]>=0 && count<=len){
t = t+gas[start%len]-cost[start%len];
start++;
count++;
}
// If you run through all the sites , Then you'll find the answer
if(count>=len){
return i;
}else{
// Otherwise, start from the next service station that you can't get to , Because starting from the service station in front , I've tried , Can't go through all sites , Think about it
i = i+count;
}
}
}
return -1;
}
}
版权声明
本文为[Sauce bear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230036003057.html
边栏推荐
- A tikv hard disk usage problem caused by GC not working caused by ticdc exception
- MySQL -- data type
- Calculation of plot ratio by ArcGIS
- ACWing春季每日一题——你知道ABC吗
- 为什么进入上一个输入框后,下一个输入框光标进不去(是只能在前部分才能点击光标进入输入框,光标点击输入框后部分不起作用)
- 随笔8:错误解决Error in readPNG(paste(location,“/“,pattern.type[i],“.png“,sep = ““)):unable to open C:/
- [image classification] reproduce senet with the shortest code. Xiaobai must be collected (keras, tensorflow2. X)
- L2-007 family real estate (25 points) and search set or graph traversal
- 【图像分类】——来来来,干了这碗EfficientNet实战(Pytorch)
- ArcGIS urban living area land suitability evaluation (III)
猜你喜欢

深度学习基础学习-RNN与LTSM

2.58 - write the program is little endian, which returns 1 when compiled and run on the small end method machine and 0 when compiled and run on the large end method machine. This program should be abl

信息系统项目管理-立项管理

ArcGIS urban living area land suitability evaluation (II)

Steps to apply for a CA certificate

将 AWS S3 数据迁移至 TiDB Cloud 集群

Simple use of Excel if function

C语言 #和 ##

Calculation of plot ratio by ArcGIS

【图像分类】——来来来,干了这碗EfficientNet实战(Pytorch)
随机推荐
市场格局进入重构期,ESP频繁「召回」,中国供应商「乘势而上」
ArcMAP 使用绘图工具添加注记
Research progress of Lake remote sensing (Overview)
Mp2459 is a perfect replacement for 60v0 with power MOSFET fs2459 integrated inside 5A step-down IC
多测师杭州拱墅校区肖sir_高级金牌讲师_简历实战
2022年4月22日,第15天
Analysis and application of conditional compilation
Multi surveyor Xiao sir, Gongshu campus, Hangzhou_ Senior gold medal lecturer_ Resume practice
信息系统项目管理-立项管理
ArcGIS urban living area land suitability evaluation (II)
repeat_ map mp. find() + mp. erase()
[C#]给地球点颜色看看
leetcode 134. 加油站
L2-012 judgment on heap (25 points) (string bug to be solved)
Beginner MCU lights up the first peripheral -- led light
Application of tidb in massive transactions and real-time analysis of fast food chain enterprises
深度学习基础学习-残差
在Google工作的十年,也是我开心的十年
ArcMAP 空间连接和相交工具使用
ArcGIS urban living area land suitability evaluation (V)