当前位置:网站首页>leetcode134. gas station
leetcode134. gas station
2022-07-07 08:38:00 【No woman knocks the code in the heart, natural God!!】
One : The analects of Confucius
Two : subject
3、 ... and : Upper code ( Violent solution time out But method two is OK )
// class Solution {
// public:
// int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
// /**
// Ideas :1. Violence solution , We traverse all elements Until you can succeed Otherwise return to -1;
// */
// for(int i = 0; i < gas.size(); i++) {
// int num1 = gas[i] - cost[i];
// int index = (i+1) % cost.size();// Here we use this to realize our cycle index Represents array subscript
// while(num1 > 0 && index != i) {
// num1 += gas[index] - cost[index];
// index = (index+1) % cost.size();// When index+1 == cost.size() When We're back 0 The starting point
// }
// if(num1 >= 0 && index == i) return i;
// }
// return -1;
// }
// };
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
/** Ideas :1. If our total oil volume is greater than the oil consumption We can definitely go around */
int cur = 0;
int sum = 0;
int start = 0;
for(int i = 0; i < gas.size(); i++) {
cur += gas[i] -cost[i];
sum += gas[i] -cost[i];
if(cur < 0) {
// When we count i When If the fuel quantity is negative , We can be sure that we won't go around
start = i+1;
cur = 0;
}
}
if(sum < 0) return -1;
return start;
}
};
边栏推荐
- Implementation of navigation bar at the bottom of applet
- Data type - floating point (C language)
- PVTV2--Pyramid Vision TransformerV2学习笔记
- [Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
- Novice entry SCM must understand those things
- 2-3 lookup tree
- MES系统,是企业生产的必要选择
- Deit learning notes
- [kuangbin] topic 15 digit DP
- Qt Charts使用(重写QChartView,实现一些自定义功能)
猜你喜欢
Practice of combining rook CEPH and rainbow, a cloud native storage solution
[南京大学]-[软件分析]课程学习笔记(一)-introduction
Exercise arrangement 2.10, 11
AVL balanced binary search tree
Rainbow combines neuvector to practice container safety management
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Opencv learning notes 1 -- several methods of reading images
Open3d ISS key points
GFS分布式文件系统
The single value view in Splunk uses to replace numeric values with text
随机推荐
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
uniapp 微信小程序监测网络
Novice entry SCM must understand those things
Input of mathematical formula of obsidan
Analyzing the influence of robot science and technology development concept on Social Research
Using nocalhost to develop microservice application on rainbow
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
opencv之图像分割
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
Explore creativity in steam art design
Practice of implementing cloud native Devops based on rainbow library app
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
字符串操作
注解@ConfigurationProperties的三种使用场景
PVTV2--Pyramid Vision TransformerV2学习笔记
MES系统,是企业生产的必要选择
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
使用AGC重签名服务前后渠道号信息异常分析