-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP134.cpp
More file actions
32 lines (27 loc) · 694 Bytes
/
P134.cpp
File metadata and controls
32 lines (27 loc) · 694 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
#include "header.h"
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n=gas.size();
int remain = 0, ans = 0;
int _min = 0, _min_i = -1;
for (int i=0; i<n; i++) {
int t = gas[i]-cost[i];
remain += gas[i] - cost[i];
if (remain < _min) {
_min = remain;
_min_i = i;
}
}
if (remain < 0) return -1;
return _min_i+1;
// -2 -2 -2 3 3
}
};
int main() {
vector<int> gas, cost;
gas = {1,2,3,4,5};
cost = {3,4,5,1,2};
cout << Solution().canCompleteCircuit(gas, cost) << endl;
return 0;
}