当前位置:网站首页>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;
}
};

边栏推荐
- The single value view in Splunk uses to replace numeric values with text
- Le système mes est un choix nécessaire pour la production de l'entreprise
- GOLand idea intellij 无法输入汉字
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
- Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
- POJ - 3784 Running Median(对顶堆)
- [paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
- Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
- opencv 将16位图像数据转为8位、8转16
- [Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
猜你喜欢

rsync远程同步

PLSQL的安装和配置

Go语言中,函数是一种类型

Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
![Upload an e-office V9 arbitrary file [vulnerability recurrence practice]](/img/e7/278193cbc2a2f562270f99634225bc.jpg)
Upload an e-office V9 arbitrary file [vulnerability recurrence practice]

SSM integration

Rsync remote synchronization

如何在HarmonyOS应用中集成App Linking服务

Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster

数据分析方法论与前人经验总结2【笔记干货】
随机推荐
Ebpf cilium practice (1) - team based network isolation
Implementation of navigation bar at the bottom of applet
AVL平衡二叉搜索树
Interpreting the practical application of maker thinking and mathematics curriculum
opencv 将16位图像数据转为8位、8转16
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
rsync远程同步
mysql分区讲解及操作语句
Open3D ISS关键点
Using nocalhost to develop microservice application on rainbow
Train your dataset with swinunet
Analysis of maker education in innovative education system
Virtual address space
使用AGC重签名服务前后渠道号信息异常分析
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
Give full play to the wide practicality of maker education space
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
POJ - 3616 Milking Time(DP+LIS)
Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"