forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.cpp
More file actions
38 lines (29 loc) · 810 Bytes
/
main2.cpp
File metadata and controls
38 lines (29 loc) · 810 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
/// Source : https://leetcode.com/problems/gas-station/
/// Author : liuyubobobo
/// Time : 2019-03-16
#include <iostream>
#include <vector>
using namespace std;
/// Simulation
/// This solution is more intuitive, see Leetcode Official Solution for details:
/// https://leetcode.com/problems/gas-station/solution/
///
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int n = gas.size();
int res = 0, total = 0, cur = 0;
for(int i = 0; i < n; i ++){
cur += gas[i] - cost[i];
total += gas[i] - cost[i];
if(total <= 0) res = (i + 1) % n, total = 0;
}
if(cur < 0) return -1;
return res;
}
};
int main() {
return 0;
}