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

边栏推荐
- Give full play to the wide practicality of maker education space
- Implementation method of data platform landing
- Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
- Tips for using jeditabletable
- 【无标题】
- Opencv learning note 3 - image smoothing / denoising
- Interpreting the practical application of maker thinking and mathematics curriculum
- 下载和安装orcale database11.2.0.4
- Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
- Go语言中,函数是一种类型
猜你喜欢

归并排序和非比较排序

南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
![[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology](/img/2a/01db1b84c26502c851786aaca56abe.jpg)
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology

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

Golang compilation constraint / conditional compilation (/ / +build < tags>)

关于基于kangle和EP面板使用CDN

Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms

Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications

調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error

Componentspace2022, assertions, protocols, bindings, and configuration files
随机推荐
[Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
Golang 编译约束/条件编译 ( // +build <tags> )
National SMS center number inquiry
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
Splunk query CSV lookup table data dynamic query
grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
AVL平衡二叉搜索树
Laravel8 uses passport login and JWT (generate token)
[Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Data type - integer (C language)
Mock.js用法详解
Practice of combining rook CEPH and rainbow, a cloud native storage solution
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
下载和安装orcale database11.2.0.4
Opencv learning note 3 - image smoothing / denoising
2-3查找樹
Practice of implementing cloud native Devops based on rainbow library app
opencv之图像分割