当前位置:网站首页>134. 加油站
134. 加油站
2022-06-09 14:10:00 【anieoo】
原题链接:134. 加油站
solution:
本题先考虑暴力做法,暴力做法就是枚举每一个点为起点,判断它能否绕环形路一周,时间复杂度O(n^2),肯定会超时,下面贴上暴力做法代码:
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
for (int i = 0, j; i < n;i++) { // 枚举起点
int left = 0; //新的起点重新计算剩余油量
for (j = 0; j < n; j ++ ) {
int k = (i + j) % n;
left += gas[k] - cost[k];
if (left < 0) break; //剩余油量小于0,则跳出本次循环
}
if (j == n) return i;
}
return -1;
}
}; 于是得考虑一种有效的方法,对暴力做法进行优化。可以这样考虑假设起点为0,当你枚举到第4个加油站的时候,剩余油量小于0,代表以起点0的加油站不能走完整个路程。
如果按照暴力的做法,现在肯定会去枚举第1个加油站,而我们对代码进行优化,让他直接去枚举第5个加油站就行了。
原理:以0为起点,经过第1,2,3个加油站的时候,剩余油量肯定都是大于0的,在这种情况下都无法走到第4个加油站,如果以1,2,3为起点,剩余油量为0,那就更不可能走完了。
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
for (int i = 0, j; i < n;) { // 枚举起点
int left = 0; //新的起点重新计算剩余油量
for (j = 0; j < n; j ++ ) {
int k = (i + j) % n;
left += gas[k] - cost[k];
if (left < 0) break; //剩余油量小于0,则跳出本次循环
}
if (j == n) return i;
i = i + j + 1;
}
return -1;
}
};双指针:将环形扩展到2n数组
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
vector<int> sum = vector<int> (n * 2, 0);
//求解每个位置的花费
for(int i = 0;i < 2 * n;i++) {
sum[i] = gas[i % n] - cost[i % n];
}
int start = 0,end = 0,tot = 0; //起点,终点,当前油量
while(start < n && end < 2 * n) {
tot += sum[end];
while(tot < 0) {
tot -= sum[start];
start++;
}
if(end - start + 1 == n) return start;
end++;
}
return -1;
}
};边栏推荐
- Avenue to simplicity | how to configure self attention for vit is the most reasonable?
- 薪酬不变,每周只上四天班,英国试行全球最大规模“四天工作制”
- Hongmeng transplantation i.mx6ull (V) overview of transplantation
- Hongmeng transplants the compiling system of i.mx6ull (VII) liteos-a
- Best practices for WSL installation
- 临时全局变量和IRISTEMP数据库
- Les salaires restent inchangés, avec seulement quatre jours de travail par semaine, et le Royaume - Uni expérimente la plus grande « Semaine de travail de quatre jours » au monde.
- Software configuration item change and baseline change
- MySQL transaction
- One month after joining Tencent for outsourcing, I left
猜你喜欢

分布式限流之基于Sentinel实现的限流漫谈(一)-概述

Vscode markdown 添加、粘贴、导入图片

< test> basic knowledge interview test site

鸿蒙移植i.mx6ull (七) Liteos-a的编译系统

文化和自然遗产日,任务空投来了

@EnableFeignClients注解源码解析

Hongmeng transplants the compiling system of i.mx6ull (VII) liteos-a

Vscade markdown add, paste and import pictures

In WinCC, how to use C script to set + reset + negate variables?

Meanings of 10 important concepts and charts in Data Science
随机推荐
Solution to the problem that the WordPress address (URL) cannot be opened after modification
Three years of licensing, 5g network in-depth coverage application is integrated into thousands of industries
Leetcode 2001. 可互换矩形的组数(暴力枚举失败了)
电容电感阻抗模型分析和电源解耦电容选取经验
Hongmeng porting i.mx6ull (VIII) adding a board
Is it safe to open an account in an external market or an internal market??
C# 计算两个时间间隔
How greatsql is a popular open source database in China
智慧农业小麦室外病虫害防治规范,北斗农业
Introduction to assembly language - instruction and addressing
【Leetcode刷题记录】分类整理
Leetcode longest sequence
Hardware foundation - analog circuit
Hongmeng porting i.mx6ull (VI) kconfig_ GCC_ Mkefile
Nerf neural radiation field eccv2020
mysql学习
Implementation scheme of RTSP video stream real-time playing on web end of webcam
今天19:30 | 图形学专场—中国科学院计算技术研究所高林老师团队
PhD Debate | 自监督学习在推荐系统中的应用
硬件基础——模拟电路