当前位置:网站首页>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;
}
};
边栏推荐
- 如何理解分布式架构和微服务架构呢
- National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
- MES system is a necessary choice for enterprise production
- Go语言中,函数是一种类型
- Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
- Laravel8 uses passport login and JWT (generate token)
- POJ - 3784 running medium
- String operation
- 如何在HarmonyOS应用中集成App Linking服务
- Mock.js用法详解
猜你喜欢
Analysis of maker education in innovative education system
Rainbow combines neuvector to practice container safety management
联想混合云Lenovo xCloud:4大产品线+IT服务门户
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
详解华为应用市场2022年逐步减少32位包体上架应用和策略
AVL平衡二叉搜索树
Virtual address space
Componentspace2022, assertions, protocols, bindings, and configuration files
关于基于kangle和EP面板使用CDN
Xcit learning notes
随机推荐
String operation
Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
Train your dataset with swinunet
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
Using nocalhost to develop microservice application on rainbow
[kuangbin] topic 15 digit DP
Download and install orcale database11.2.0.4
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Learn how to compile basic components of rainbow from the source code
How to realize the high temperature alarm of the machine room in the moving ring monitoring system
Input and output of floating point data (C language)
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
国标GB28181协议视频平台EasyGBS新增拉流超时配置
Open3D ISS关键点
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
2-3 lookup tree
In go language, function is a type
Iptables' state module (FTP service exercise)
Open3d ISS key points