当前位置:网站首页>LeetCode_ 134_ gas station
LeetCode_ 134_ gas station
2022-07-26 18:24:00 【Fitz1318】
Topic link
Title Description
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 .
Tips :
gas.length == ncost.length == n1 <= n <= 10^50 <= gas[i], cost[i] <= 10^4
Their thinking
The law of greed
- The remaining quantity of each gas station
rest[i] = gas[i] - cost[i] ifrom 0 Start accumulatingrest[i], He jizuocurSum, oncecurSum < 0, explain[0,i]No interval can be used as the starting position , Start fromi+1Count up ,curSumIt is also necessary to reset synchronously and accumulate again- If there is a larger negative number , It's about to be updated
i
AC Code
class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int start = 0;
int curSum = 0;
int totalSum = 0;
for (int i = 0; i < gas.length; i++) {
curSum += gas[i] - cost[i];
totalSum += gas[i] - cost[i];
if (curSum < 0) {
start = i + 1;
curSum = 0;
}
}
if (totalSum < 0) {
return -1;
}
return start;
}
}
边栏推荐
- LeetCode_134_加油站
- Drools basic grammar
- OpenGL中的视差贴图的着色器代码
- Become a test / development programmer, Xiao Zhang: reality is coming
- The step jumping expansion problem of sword finger offer
- Linux安装mysql8.0.29详细教程
- LeetCode50天刷题计划(Day 4—— 最长回文子串 14.00-16:20)
- LeetCode 0139. 单词拆分
- The third day of SSM practice_ Paging assistant_ Security framework
- 老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破
猜你喜欢

ssm练习第四天_获取用户名_用户退出_用户crud_密码加密_角色_权限

5、 Parameter server principle, code implementation
![[unity3d] rocker](/img/b7/40643a2676b251c185ce58840f7581.png)
[unity3d] rocker

The second set of 2020 American Asian individual match

ALV屏幕输入选项学习

Relative path and absolute path

Maximum sum of continuous subarray of sword finger offer (2)

8.1 Diffie Hellman key exchange

SSH based online mall

Nailing third-party service provider application ISV application development and listing tutorial
随机推荐
9、 Alternative implementation of client for service communication
SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
贪心——455. 分发饼干
Leetcode 0137. number II that appears only once
Oracle day 2 (Views, indexes, PLSQL, cursors, stored procedures and stored functions, triggers, JDBC access stored procedures and stored functions)
Detailed explanation of openwrt's feeds.conf.default
Online stock trading, where to choose to open an account is safer?
Point cloud target detection Kitti dataset bin file visualization, one-stop solution
8.1 Diffie Hellman key exchange
剑指offer 跳台阶扩展问题
8.1 Diffie-Hellman密钥交换
菜鸟 CPaaS 平台微服务治理实践
[a little knowledge] thread pool
《敏捷宣言》四大价值观,十二大原则
Sword finger offer regular expression matching
China polyisobutylene Market Research and investment value report (2022 Edition)
【静态代码质量分析工具】上海道宁为您带来SonarSource/SonarQube下载、试用、教程
你适合做自动化 测试吗?
Oracle第二天(视图、索引、plsql、游标、存储过程和存储函数、触发器、jdbc访问存储过程和存储函数)
剑指offer 连续子数组的最大和(二)