-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas_station.cc
67 lines (53 loc) · 1.35 KB
/
gas_station.cc
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
// Copyright 2014 kevinew@leetcode Inc. All Rights Reserved.
// Solve:
// a[] = {1 2 -3 5 6 -6}
// so we can consider b[] = [a[] a[]], just is joint with 2 a.
// Find the n(a)-length, sum is not less than 0.
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
#define VI vector<int>
#define VS vector<string>
// Max sum of sub array - Just use 2 same array to concat.
// 1 -1 5 6 => 1 -1 5 6 1 -1 5 6
class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int n = gas.size();
vector<int> a;
for (int i = 0; i < n; ++i) gas[i] -= cost[i];
// concat with 2 same array.
a.insert(a.end(), gas.begin(), gas.end());
a.insert(a.end(), gas.begin(), gas.end());
int start, count, sum;
start = 0;
count = 0;
sum = 0;
for (int i = 0; i < a.size(); ++i) {
sum += a[i];
count++;
if (sum < 0) {
count = 0;
sum = 0;
start = i + 1;
continue;
}
if (count == n) return start;
}
return -1;
}
};
int main(int argc, char **argv) {
Solution s;
vector<int> ratings(5, 3);
ratings[0] = 6;
ratings[1] = 2;
ratings[2] = 8;
ratings[3] = 9;
ratings[4] = 7;
cout << s.candy(ratings) << endl;
return 0;
}