-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularLine.hpp
58 lines (51 loc) · 1.46 KB
/
CircularLine.hpp
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
#include <iostream>
#include <limits>
#include <vector>
using namespace std;
class CircularLine
{
public:
int sum = 0;
int longestTravel(vector<int> t) {
int min = 0;
int maxMin = 0;
int x,y;
int total = 0;
// Get total sum of t
for (int i=0; i<t.size(); i++) {
total += t[i];
}
// cout << "Total size " << total << endl;
// Find maximum of the minimums
for (int i=0; i<t.size()-1; i++) {
// reset
x = 0;
y = 0;
for (int j=i+1; j<t.size(); j++) {
sum = 0;
// Find the 2 distances between station i and j
x = calcSum(t, i, j);
y = total - x;
// Get the minimum distance
min = (x < y) ? x : y;
// cout << "i " << i << " j " << j << " x " << x << " y " << y << " min " << min << endl;
// Get the maximum of the minimums
maxMin = (min > maxMin) ? min : maxMin;
// cout << "maxMin " << maxMin << endl;
}
}
return maxMin;
}
int calcSum(vector<int> t, int i, int j) {
// base case
if (i+1 == j) {
sum += t[i];
// cout << "sum " << sum << endl;
return sum;
}
sum += t[j-1];
// cout << "sum " << sum << endl;
calcSum(t, i, j-1);
return sum;
}
};