-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path134.cpp
More file actions
42 lines (39 loc) · 1010 Bytes
/
134.cpp
File metadata and controls
42 lines (39 loc) · 1010 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
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
Solution() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int length = gas.size();
int gasAvailable = 0;
int gasUsed = 0;
int paid = 0;
int startingPoint = 0;
for (int i=0; i<length; ++i) {
gasUsed += gas[i];
paid += cost[i];
gasAvailable += gas[i] - cost[i];
if (gasAvailable < 0) {
gasAvailable = 0;
startingPoint = i + 1;
}
}
return paid > gasUsed ? -1 : startingPoint;
}
};
int
main()
{
Solution solution;
vector<int> gas = {1, 2, 3, 4, 5};
vector<int> cost = {3, 4, 5, 1, 2};
cout << solution.canCompleteCircuit(gas, cost) << endl;
gas = {2, 3, 4};
cost = {3, 4, 3};
cout << solution.canCompleteCircuit(gas, cost) << endl;
return 0;
}