-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGas Station.cpp
51 lines (48 loc) · 932 Bytes
/
Gas Station.cpp
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
43
44
45
46
47
48
49
50
51
// say Alhamdulillah
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int sum = 0, ans = 0;
int s1 = 0, s2 = 0;
for (int i = 0; i < gas.size(); i++) {
s1 += gas[i];
s2 += cost[i];
sum += gas[i] - cost[i];
if (sum < 0) {
ans = i + 1;
sum = 0;
}
}
if (ans >= gas.size() || s1 < s2)
return -1;
else
return ans;
}
};
int main() {
vector<int> v, v2;
int n, k;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
}
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v2.push_back(x);
}
// string s;
// cin >> s;
// vector<int>
Solution sl;
int ans = sl.canCompleteCircuit(v, v2);
// for (auto x : ans) {
// cout << x << " ";
// }
cout << ans;
// cout << ans[0] << " " << ans[1] << endl;
}