-
Notifications
You must be signed in to change notification settings - Fork 8
/
leetcode134_gas_station.cpp
83 lines (70 loc) · 1.95 KB
/
leetcode134_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @file leetcode134_gas_station.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/07/20 14:44
* @brief https://leetcode.com/problems/gas-station
*
**/
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int total_gas = 0;
int total_cost = 0;
int stations = gas.size();
for (int i = 0; i < stations; ++i) {
total_gas += gas[i];
total_cost += cost[i];
}
if (total_gas < total_cost) {
return -1;
}
for (int start = 0; start < stations;) {
int surplus = 0;
bool ok = true;
for (int i = 0; i < stations; ++i) {
int id = (start + i) % stations;
surplus += gas[id] - cost[id];
if (surplus < 0) {
start = id + 1;
ok = false;
break;
}
}
if (ok) {
return start;
}
}
return -1;
}
};
int main() {
while (1) {
int n = 0;
n = 0;
std::cout << "Number of elements (Ctrl-C to exit): " << std::endl;
if (!(std::cin >> n)) {
return 0;
}
std::cout << n << " gas volumes: " << std::endl;
std::vector<int> gas;
int ele;
for (int i = 0; i < n; ++i) {
std::cin >> ele;
gas.push_back(ele);
}
std::cout << n << " costs: " << std::endl;
std::vector<int> cost;
for (int i = 0; i < n; ++i) {
std::cin >> ele;
cost.push_back(ele);
}
Solution solution;
auto ret = solution.canCompleteCircuit(gas, cost);
std::cout << ret << std::endl;
}
return 0;
}