当前位置:网站首页>134. gas station
134. gas station
2022-06-10 03:23:00 【anieoo】
Original link :134. Gas station
solution:
This topic first considers violent practices , Violence is to enumerate every point as a starting point , Judge whether it can go round the ring road , Time complexity O(n^2), It's bound to time out , The code of violent practices is posted below :
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
for (int i = 0, j; i < n;i++) { // Enumeration starting point
int left = 0; // Recalculate the remaining oil volume from a new starting point
for (j = 0; j < n; j ++ ) {
int k = (i + j) % n;
left += gas[k] - cost[k];
if (left < 0) break; // Remaining oil is less than 0, Then jump out of this cycle
}
if (j == n) return i;
}
return -1;
}
}; So we have to consider an effective method , Optimize violent practices . Consider this by assuming that the starting point is 0, When you enumerate to 4 At a gas station , Remaining oil is less than 0, Represents the starting point 0 You can't walk all the way to the gas station .
If the practice of violence , Now I will definitely enumerate the 1 A gas station , And we optimize the code , Ask him to enumerate the number 5 Just a gas station .
principle : With 0 As a starting point , After the first 1,2,3 At a gas station , The remaining oil volume must be greater than 0 Of , In this case, you can't go to the 4 A gas station , If the 1,2,3 As a starting point , The remaining oil volume is 0, Then it is even more impossible to finish .
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
for (int i = 0, j; i < n;) { // Enumeration starting point
int left = 0; // Recalculate the remaining oil volume from a new starting point
for (j = 0; j < n; j ++ ) {
int k = (i + j) % n;
left += gas[k] - cost[k];
if (left < 0) break; // Remaining oil is less than 0, Then jump out of this cycle
}
if (j == n) return i;
i = i + j + 1;
}
return -1;
}
};Double pointer : Extend the ring to 2n Array
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
vector<int> sum = vector<int> (n * 2, 0);
// Solve the cost of each location
for(int i = 0;i < 2 * n;i++) {
sum[i] = gas[i % n] - cost[i % n];
}
int start = 0,end = 0,tot = 0; // The starting point , End , Current oil volume
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;
}
};边栏推荐
- Understand IPS and IDS
- Xmake v2.6.6 發布,分布式編譯和緩存支持
- IDEA同一套代码启动多个服务
- Using the responsibility chain pattern to reconstruct the original code
- Analysis of the meaning of autocorrelation function / autocovariance function in different fields
- Basic data types and sizeof understanding
- 重构--消除重复代码
- The playback capacity of a single video is more than 8000w, so hard core cooking is so superior
- Refactoring --inline
- P1082 [NOIP2012 提高组] 同余方程
猜你喜欢

Data Lake solutions of various manufacturers

小功能实现(三)字符串分割中括号中的内容

Multithreaded usage scenarios

Refactoring --inline

Online salon | open source show -- Application Practice of database technology

開源框架對Range模式的支持

cmake记录

The playback capacity of a single video is more than 8000w, so hard core cooking is so superior

Prise en charge du mode range par le cadre Open Source
![[mui+flask+mongodb+hbuilderx] detailed explanation of the answer integration logic of app development](/img/17/c099b7b34560d2c8e6ff0637921168.png)
[mui+flask+mongodb+hbuilderx] detailed explanation of the answer integration logic of app development
随机推荐
switch case语法
重构--代码坏味道
一个通用的es聚合查询方法
里程碑事件丨.NET MAUI 正式发布
TiDB经验分享01
Luogu p1902 assassinating Ambassador (two points | BFS)
Refactoring technique --extract class
零拷贝原理详解
What are the assessment units of Shaanxi Xi'an insurance? Where can I find it?
Switch case syntax
多线程的实现
A general es aggregation query method
Vscade C language code ctrl+ left key cannot jump to definition
Why is the denominator of sample variance n-1 (unbiased estimation)?
三个月GMV6000w+,盘点家纺行业打造爆款的关键
yum使用总结
IDE problem (I) wechat developer tool cannot be opened
開源框架對Range模式的支持
Wang Xing, Zhang Yong, Xu Lei, who can win the local e-commerce?
C Looooops(拓展欧几里得)