forked from arshdeep/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowerGarden.cpp
More file actions
189 lines (158 loc) · 4.83 KB
/
FlowerGarden.cpp
File metadata and controls
189 lines (158 loc) · 4.83 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// topcoder.cpp
// test
//
// Created by asingh on 11/19/14.
// Copyright (c) 2014 asingh. All rights reserved.
//
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/* http://topcoder.bgcoder.com/print.php?id=393 */
#define SIZEOFINTARRAY(a) sizeof(a)/sizeof(int)
class FlowerGarden
{
public:
vector<int> getOrdering(vector<int> height, vector<int> bloom, vector<int> wilt);
};
vector<int> FlowerGarden::getOrdering(vector<int> height, vector<int> bloom, vector<int> wilt)
{
size_t len = height.size();
vector<int> result;
vector<int> used(len);
for (size_t i = 0; i < len; ++i)
{
size_t maxIndex = -1;
int maxHeight = -1;
for (size_t j = 0; j < len; ++j) {
if (!used[j])
{
bool found = true;
for (size_t k = 0; k < len && found; ++k) {
if (!used[k])
{
bool isBlocking = (wilt[j] >= bloom[k] && bloom[j] <= wilt[k])
|| (wilt[j] >= wilt[k] && bloom[j] <= wilt[k]);
if (height[j] > height[k] && isBlocking)
{
found = false;
break;
}
}
}
if (found && height[j] > maxHeight)
{
maxIndex = j;
maxHeight = height[j];
}
}
}
if (maxIndex != -1)
{
used[maxIndex] = true;
result.push_back(maxHeight);
}
}
return result;
}
void TEST(vector<int> height, vector<int> bloom, vector<int> wilt, vector<int> expected)
{
clock_t start, end;
double cpu_time_used;
start = clock();
FlowerGarden flowerGarden;
vector<int> result = flowerGarden.getOrdering(height, bloom, wilt);
end = clock();
cpu_time_used = ((double) (end - start));
cout<<"Time taken : "<<cpu_time_used<<endl;
assert( result.size() == expected.size() &&
std::equal(result.begin(), result.end(),
expected.begin()) );
}
vector<int> convert(int *list, int size)
{
vector<int> vect;
for (int idx = 0; idx < size; ++idx)
{
vect.push_back(list[idx]);
}
return vect;
}
void Test1()
{
int height[] = {730,882,841,120,789};
int bloom[] = {51,234,354,273,115};
int wilt[] = {214, 319,363,331,251};
int expected[] = {841, 730, 789, 120, 882};
TEST( convert(height, sizeof(height)/sizeof(int)),
convert(bloom, sizeof(bloom)/sizeof(int)),
convert(wilt, sizeof(wilt)/sizeof(int)),
convert(expected, sizeof(expected)/sizeof(int)) );
}
void Test2()
{
int height[] = {5,4,3,2,1};
int bloom[] = {1,1,1,1,1};
int wilt[] = {365,365,365,365,365};
int expected[] = {1, 2, 3, 4, 5};
TEST( convert(height, sizeof(height)/sizeof(int)),
convert(bloom, sizeof(bloom)/sizeof(int)),
convert(wilt, sizeof(wilt)/sizeof(int)),
convert(expected, sizeof(expected)/sizeof(int)) );
}
void Test3()
{
int height[] = {5,4,3,2,1};
int bloom[] = {1,5,10,15,20};
int wilt[] = {4,9,14,19,24};
int expected[] = {5, 4, 3, 2, 1};
TEST( convert(height, sizeof(height)/sizeof(int)),
convert(bloom, sizeof(bloom)/sizeof(int)),
convert(wilt, sizeof(wilt)/sizeof(int)),
convert(expected, sizeof(expected)/sizeof(int)) );
}
void Test4()
{
int height[] = {5,4,3,2,1};
int bloom[] = {1,5,10,15,20};
int wilt[] = {5,10,15,20,25};
int expected[] = {1, 2, 3, 4, 5 };
TEST( convert(height, sizeof(height)/sizeof(int)),
convert(bloom, sizeof(bloom)/sizeof(int)),
convert(wilt, sizeof(wilt)/sizeof(int)),
convert(expected, sizeof(expected)/sizeof(int)) );
}
void Test5()
{
int height[] = {3,2,5,4};
int bloom[] = {1,2,11,10};
int wilt[] = {4,3,12,13};
int expected[] = {4, 5, 2, 3 };
TEST( convert(height, sizeof(height)/sizeof(int)),
convert(bloom, sizeof(bloom)/sizeof(int)),
convert(wilt, sizeof(wilt)/sizeof(int)),
convert(expected, sizeof(expected)/sizeof(int)) );
}
void Test6()
{
int height[] = {1,2,3,4,5,6};
int bloom[] = {1,3,1,3,1,3};
int wilt[] = {2,4,2,4,2,4};
int expected[] = {2, 4, 6, 1, 3, 5};
TEST( convert(height, sizeof(height)/sizeof(int)),
convert(bloom, sizeof(bloom)/sizeof(int)),
convert(wilt, sizeof(wilt)/sizeof(int)),
convert(expected, sizeof(expected)/sizeof(int)) );
}
int main(int argc, const char * argv[]) {
// insert code here...
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
cout<<"success";
return 0;
}