当前位置:网站首页>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;
}
};
边栏推荐
- Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
- uniapp 微信小程序监测网络
- [南京大学]-[软件分析]课程学习笔记(一)-introduction
- 详解华为应用市场2022年逐步减少32位包体上架应用和策略
- 【微信小程序:缓存操作】
- Ebpf cilium practice (1) - team based network isolation
- Rainbow combines neuvector to practice container safety management
- National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
- POJ - 3784 running medium
- [kuangbin] topic 15 digit DP
猜你喜欢
[hard core science popularization] working principle of dynamic loop monitoring system
下载和安装orcale database11.2.0.4
SSM integration
Automatic upgrading of database structure in rainbow
Teach you how to select PCB board by hand (II)
How to integrate app linking services in harmonyos applications
Laravel8 uses passport login and JWT (generate token)
Low success rate of unit test report
Explore creativity in steam art design
Implementation of navigation bar at the bottom of applet
随机推荐
The field value in Splunk subquery fuzzy matching CSV is*
如何在快应用中实现滑动操作组件
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
What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
详解华为应用市场2022年逐步减少32位包体上架应用和策略
【微信小程序:缓存操作】
2-3查找樹
Interface as a parameter (interface callback)
POJ - 3616 Milking Time(DP+LIS)
POJ - 3784 running medium
Give full play to the wide practicality of maker education space
Opencv learning notes II - basic image operations
GFS分布式文件系统
Practice of implementing cloud native Devops based on rainbow library app
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
为什么要选择云原生数据库
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
[kuangbin]专题十五 数位DP
The single value view in Splunk uses to replace numeric values with text
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration