-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgastation.cpp
More file actions
37 lines (29 loc) · 732 Bytes
/
gastation.cpp
File metadata and controls
37 lines (29 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//leetcode Gas Station 134 Medium challenge
//https://leetcode.com/problems/gas-station/
#include <vector>
using namespace std;
class Solution
{
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost)
{
int total_gas = 0, total_cost = 0, tank = 0, start = 0;
for (int i = 0; i < gas.size(); i++)
{
total_gas += gas[i];
total_cost += cost[i];
}
if (total_gas < total_cost)
return -1;
for (int i = 0; i < gas.size(); i++)
{
tank += gas[i] - cost[i];
if (tank < 0)
{
start = i + 1;
tank = 0;
}
}
return start;
}
};