当前位置:网站首页>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;
}
}
边栏推荐
- Win10 wireless connection cannot input password characters, and it will be stuck as soon as it is input
- Rookie cpaas platform microservice governance practice
- 面试OPPO,16道题甩过来,我人傻了
- 4、 Service communication principle, code implementation
- [ Kitex 源码解读 ] 服务发现
- What is the PMP exam outline in 2022?
- 10、 Implementation of parameter modification of parameter server
- LeetCode50天刷题计划(Day 5—— 最长回文子串 10.50-13:00)
- 隐私计算基础组件系列-混淆电路
- The first day of Oracle (review and sort out the common knowledge points of development)
猜你喜欢

It is said that the salary of Alibaba P7 is really fragrant

【Unity3D】摇杆

LeetCode50天刷题计划(Day 1—— 两数相加 11.00-12.30)

College personnel management system based on jsp+servlet

【数字IC】深入浅出理解AXI-Lite协议

Oracle第二天(视图、索引、plsql、游标、存储过程和存储函数、触发器、jdbc访问存储过程和存储函数)

Rookie cpaas platform microservice governance practice

Redis主从复制,读写分离,哨兵模式

The third day of SSM practice_ Paging assistant_ Security framework

分布式链路追踪Jaeger在Golang中的使用
随机推荐
Understanding service governance in distributed development
我要开中信的证券账户找渠道的经理开安全吗?
剑指offer 正则表达式匹配
PS_ 2_ layer
2022河南萌新联赛第(三)场:河南大学
Point cloud target detection Kitti dataset bin file visualization, one-stop solution
Day 4 of SSM practice_ Get user name_ User exit_ User CRUD_ Password encryption_ Roles_ jurisdiction
The second set of 2020 American Asian individual match
Relative path and absolute path
Continue to work hard on your skills, and the more you learn, the more you will learn
网上炒股,选择在哪里开户比较安全呢?
Redis主从复制,读写分离,哨兵模式
Leetcode 0139. word splitting
链表-反转链表
Laozi cloud and Fuxin Kunpeng achieved a major breakthrough in 3D ofd 3D format documents for the first time
During the oppo interview, 16 questions were thrown over. I was stupid
openssl
贪心——455. 分发饼干
IrrKlang音频库的下载和配置
跟我学 UML 系统建模