generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.c
50 lines (42 loc) · 1.07 KB
/
solution.c
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
#include <stdbool.h>
int captureForts(int* forts, int fortsSize) {
for (int i = fortsSize - 1; i > 0; --i) {
if (forts[i] == 0) continue;
int maxCapture = 0;
int possibleCapture = 0;
bool prevEmpty = forts[i] == -1;
while (--i > 0) {
switch (forts[i]) {
case -1:
if (!prevEmpty) {
prevEmpty = true;
if (possibleCapture > maxCapture) maxCapture = possibleCapture;
}
possibleCapture = 0;
break;
case 0:
++possibleCapture;
break;
case 1:
if (prevEmpty) {
prevEmpty = false;
if (possibleCapture > maxCapture) maxCapture = possibleCapture;
}
possibleCapture = 0;
}
}
switch (forts[i]) {
case -1:
if (!prevEmpty && possibleCapture > maxCapture) {
maxCapture = possibleCapture;
}
break;
case 1:
if (prevEmpty && possibleCapture > maxCapture) {
maxCapture = possibleCapture;
}
}
return maxCapture;
}
return 0;
}